Skip to content

Instantly share code, notes, and snippets.

@maxehmookau
Created January 14, 2016 12:02
Show Gist options
  • Save maxehmookau/8d73ff2d7179c9d4039e to your computer and use it in GitHub Desktop.
Save maxehmookau/8d73ff2d7179c9d4039e to your computer and use it in GitHub Desktop.
Before & After Ruby Metaprogramming
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
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