Module: Jason::Relation::ClassMethods
- Defined in:
- lib/jason/relation.rb
Instance Method Summary collapse
- #belongs_to(*args) ⇒ Object
- #has_many(*args) ⇒ Object
- #reflect_on_relation(relation_name) ⇒ Object
- #reflections ⇒ Object
Instance Method Details
#belongs_to(*args) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/jason/relation.rb', line 82 def belongs_to(*args) relation_name = args.shift.to_s = args.empty? ? {} : args.shift class_name = .fetch(:class, relation_name).capitalize reflections << Reflection::Base.new(:name => relation_name, :class_name => class_name, :type => "belongs_to") ivar_name = "#{relation_name}_id".to_sym attribute ivar_name, String method_definition_setter = <<-RUBY def #{relation_name}=(obj) self.send("#{ivar_name}=", obj.id) self.instance_variable_set("@#{relation_name}",obj) self.reload_attributes end RUBY method_definition_getter = <<-RUBY def #{relation_name} relation_obj = self.instance_variable_get("@#{relation_name}") finder = ->() do klass = Module.const_get("#{class_name}".to_sym) relation_obj = klass.find(self.send("#{ivar_name}")) self.instance_variable_set("@#{relation_name}", relation_obj) return relation_obj end relation_obj ||= finder.call end RUBY class_eval method_definition_setter, "relation.rb", 63 class_eval method_definition_getter, "relation.rb", 72 end |
#has_many(*args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jason/relation.rb', line 26 def has_many(*args) relation_name = args.shift.to_s = args.empty? ? {} : args.shift class_name = .fetch(:class, relation_name.downcase.singularize).capitalize reflections << Reflection::Base.new(:name => relation_name, :class_name => class_name, :type => "has_many") ivar_name = "#{relation_name}_ids".to_sym attribute ivar_name, String method_definition_getter = <<-RUBY def #{relation_name} # find all children in *.json if included. # Otherwise return empty array. relation_objects = self.instance_variable_get("@#{relation_name}") finder = ->() do objects = [] object_ids = self.send("#{ivar_name}") unless object_ids.nil? or object_ids.empty? object_ids.split(Jason::has_many_separator).each do |object_id| begin klass = Module.const_get("#{class_name}".to_sym) objects << klass.find(object_id) rescue next end end end self.instance_variable_set("@#{relation_name}", objects) return objects end relation_objects ||= finder.call end RUBY method_definition_setter = <<-RUBY def #{relation_name}=(objects) self.send("#{ivar_name}=", objects.map(&:id).join(Jason::has_many_separator)) self.instance_variable_set("@#{relation_name}",objects) self.reload_attributes end RUBY class_eval method_definition_getter class_eval method_definition_setter end |
#reflect_on_relation(relation_name) ⇒ Object
22 23 24 |
# File 'lib/jason/relation.rb', line 22 def reflect_on_relation(relation_name) reflections.detect{|reflection| reflection.name.eql?(relation_name.to_s)} end |
#reflections ⇒ Object
18 19 20 |
# File 'lib/jason/relation.rb', line 18 def reflections @reflections ||= [] end |