Created
January 14, 2016 12:02
-
-
Save maxehmookau/8d73ff2d7179c9d4039e to your computer and use it in GitHub Desktop.
Before & After Ruby Metaprogramming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HierarchyProcessor | |
attr_reader :input, :user | |
def initialize(input, user) | |
raise ArgumentError, "user expected User, was #{ user.class }" unless user.is_a?(User) | |
@input = JSON.parse(input) | |
@user = user | |
end | |
[:service_groups, :locations, :roles, :employment_types].each do |type| | |
define_method(type) do | |
class_name = type.to_s.classify.constantize | |
class_name.where(id: @input[type.to_s].map{ |s| s['id'] }) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HierarchyProcessor | |
attr_reader :input, :user | |
def initialize(input, user) | |
raise ArgumentError, "user expected User, was #{ user.class }" unless user.is_a?(User) | |
@input = JSON.parse(input) | |
@user = user | |
end | |
def service_groups | |
ServiceGroup.where(id: @input['service_groups'].map{ |s| s['id'] }) | |
end | |
def locations | |
Location.where(id: @input['locations'].map{ |s| s['id'] }) | |
end | |
def roles | |
Role.where(id: @input['roles'].map{ |s| s['id'] }) | |
end | |
def employment_types | |
EmploymentType.where(id: @input['employment_types'].map{ |s| s['id'] }) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment