Module: ActiveRequest::HasMany::ClassMethods
- Defined in:
- lib/active_request/has_many.rb,
lib/active_request/belongs_to.rb
Instance Method Summary collapse
- #belongs_to(association, options = nil) ⇒ Object
- #belongs_tos ⇒ Object
- #build_belongs_tos(alloc) ⇒ Object
- #build_has_manys(alloc) ⇒ Object
- #has_many(association, options = nil) ⇒ Object
- #has_manys ⇒ Object
Instance Method Details
#belongs_to(association, options = nil) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/active_request/belongs_to.rb', line 8 def belongs_to(association, = nil) @belongs_tos ||= [] class_name = !.nil? && ![:class_name].nil? ? [:class_name] : (association.to_s.split.map(&:capitalize)*' ') foreign_key = !.nil? && ![:foreign_key].nil? ? [:foreign_key] : "#{association}_id" @belongs_tos << { association: association, class_name: class_name, foreign_key: foreign_key } end |
#belongs_tos ⇒ Object
15 16 17 |
# File 'lib/active_request/belongs_to.rb', line 15 def belongs_tos @belongs_tos end |
#build_belongs_tos(alloc) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_request/belongs_to.rb', line 19 def build_belongs_tos(alloc) alloc.belongs_tos.each do |many| alloc.instance_variable_set("@#{many[:foreign_key]}", nil) alloc.instance_variable_set("@#{many[:association]}", nil) define_method(many[:association]) do variable = alloc.instance_variable_get("@#{many[:association]}") if id && variable.blank? father_ojb = Object.const_get(many[:class_name]) variable = father_ojb.find send(many[:foreign_key]) send("#{many[:association]}=", variable) if variable end variable end define_method(many[:foreign_key]) do alloc.instance_variable_get("@#{many[:foreign_key]}") end define_method("#{many[:association]}=") do |association_setter| alloc.instance_variable_set("@#{many[:association]}", association_setter) end define_method("#{many[:foreign_key]}=") do |foreign_key_setter| alloc.instance_variable_set("@#{many[:foreign_key]}", foreign_key_setter) end end if alloc.belongs_tos end |
#build_has_manys(alloc) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_request/has_many.rb', line 18 def build_has_manys(alloc) alloc.has_manys.each do |many| alloc.instance_variable_set("@#{many[:association]}", []) define_method(many[:association]) do variable = alloc.instance_variable_get("@#{many[:association]}") if id && variable.blank? father_ojb = Object.const_get(many[:class_name]) father_model_name = father_ojb.model_name.pluralize self.class.base_uri("#{ActiveRequest.configuration.uri}/#{ActiveRequest.configuration.api_version}/") response = self.class.get("/#{self.class.model_name.pluralize}/#{id}/#{father_model_name}.json", headers: self.class.headers) return [] unless 200 == response.code body = JSON.parse(response.body) # TODO era para ser assim mesmo? children = body["#{self.class.model_name}/#{father_model_name}"].map { |params| father_ojb.new(params) } send("#{many[:association]}=", children) variable = children end variable end define_method("#{many[:association]}=") do |many_setter| alloc.instance_variable_set("@#{many[:association]}", many_setter) end end if alloc.has_manys end |
#has_many(association, options = nil) ⇒ Object
8 9 10 11 12 |
# File 'lib/active_request/has_many.rb', line 8 def has_many(association, = nil) @has_manys ||= [] class_name = !.nil? && ![:class_name].nil? ? [:class_name] : (association.to_s.split.map(&:capitalize)*' ').singularize @has_manys << { association: association, class_name: class_name } end |
#has_manys ⇒ Object
14 15 16 |
# File 'lib/active_request/has_many.rb', line 14 def has_manys @has_manys end |