Module: TeronAssociation
- Included in:
- Teron
- Defined in:
- lib/associations/has_one.rb,
lib/associations/has_many.rb,
lib/associations/belongs_to.rb
Overview
Associations for Has Many
Instance Method Summary collapse
-
#belongs_to(class_name, param_options = {}) ⇒ Object
The Object to be included.
-
#has_many(class_name, param_options = {}) ⇒ Object
The Object to do the including.
-
#has_one(class_name, param_options = {}) ⇒ Object
The Object to do the including.
Instance Method Details
#belongs_to(class_name, param_options = {}) ⇒ Object
The Object to be included
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 |
# File 'lib/associations/belongs_to.rb', line 4 def belongs_to(class_name, = {}) # Should Inverse Object Be Updated inverse = [:inverse] || true field "#{class_name}_id".to_sym # ================================== # Accessor # ================================== define_method(class_name) do # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize # Get Entry Id klass_obj_id = send("#{class_name}_id".to_sym) klass.find klass_obj_id end # ================================== # Add # ================================== define_method("#{class_name}=") do |klass_obj| # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize raise "Invalid #{klass}; Received #{klass_obj.class})" unless klass_obj.instance_of?(klass) send("#{class_name}_id=", klass_obj.id) save! end # ================================== # Destroy # ================================== define_method('destroy!') do return unless inverse # Get Association Name klass_single_name = self.class.name.downcase.singularize klass_plural_name = self.class.name.downcase.pluralize parent = send(class_name) return unless parent # ================================== # Has One vs Has Many # ================================== # Has one if parent.respond_to?("#{klass_single_name}_remove".to_sym) parent.send("#{klass_single_name}_remove", self) # Has Many elsif parent.respond_to?("#{klass_plural_name}_remove".to_sym) parent.send("#{klass_plural_name}_remove", self) end super() end end |
#has_many(class_name, param_options = {}) ⇒ Object
The Object to do the including
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 81 82 83 84 85 86 87 88 89 |
# File 'lib/associations/has_many.rb', line 4 def has_many(class_name, = {}) # Should Inverse Object Be Updated inverse = [:inverse] || true field "#{class_name}_ids".to_sym, default: -> { [] } # ================================== # Accessor # ================================== define_method(class_name) do # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize # Get Entry Ids klass_list = send("#{class_name}_ids".to_sym) return klass_list if klass_list.empty? klass_list.map { |x| klass.find x }.compact end # ================================== # Add # ================================== define_method("#{class_name}_add") do |klass_obj| # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize raise "Invalid #{klass}; Received #{klass_obj.class})" unless klass_obj.instance_of?(klass) klass_list = send("#{class_name}_ids".to_sym) klass_list.push klass_obj.id unless klass_list.include? klass_obj.id # Klass Many Name klass_many_name = self.class.to_s.underscore # Update Obj Association if inverse klass_obj.send("#{klass_many_name}=", self) klass_obj.save! end save! end # ================================== # Remove (Meta) # Don't think this whould be called directly # ================================== define_method("#{class_name}_remove") do |klass_obj| # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize raise "Invalid #{klass}; Received #{klass_obj.class})" unless klass_obj.instance_of?(klass) klass_list = send("#{class_name}_ids".to_sym) klass_list.delete klass_obj.id if klass_list.include? klass_obj.id # Remove Association on removed object if inverse klass_many_name = self.class.to_s.underscore klass_obj.send("#{klass_many_name}_id=", nil) end save! end # ================================== # Create # ================================== define_method("#{class_name}_create") do |opts = {}| # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize klass_obj = klass.new(opts) send("#{class_name}_add", klass_obj) klass_obj end # ================================== # Destroy All # ================================== define_method("#{class_name}_destroy_all") do send(class_name).each(&:destroy!) if inverse end end |
#has_one(class_name, param_options = {}) ⇒ Object
The Object to do the including
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/associations/has_one.rb', line 4 def has_one(class_name, = {}) # @relationships ||= [] # Should Inverse Object Be Updated inverse = if .key?(:inverse) [:inverse] else true end field "#{class_name}_id".to_sym # ================================== # Accessor # ================================== define_method(class_name) do # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize # Get Entry Id klass_one = send("#{class_name}_id".to_sym) return klass_one if klass_one.nil? klass.find klass_one end # ================================== # Add # ================================== define_method("#{class_name}=") do |klass_obj| # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize raise "Invalid #{klass}; Received #{klass_obj.class})" unless klass_obj.instance_of?(klass) send("#{class_name}_id=", klass_obj.id) save! end # ================================== # Remove (Meta) # Don't think this whould be called directly # ================================== define_method("#{class_name}_remove") do |klass_obj| # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize # Validate Correct Klass Object raise "Invalid #{klass}; Received #{klass_obj.class})" unless klass_obj.instance_of?(klass) klass_name = self.class.name.downcase.singularize # Get Entry Id # klass_one = send("#{class_name}_id".to_sym) # return klass_one if klass_one.nil? klass_obj.send("#{klass_name}_id=", nil) if inverse send("#{class_name}_id=", nil) save! end # ================================== # Create # ================================== define_method("#{class_name}_create") do |opts = {}| # Get Constant klass = class_name.to_s.capitalize.singularize.classify.constantize klass_obj = klass.new(opts) send("#{class_name}=", klass_obj) # Klass Has One Name klass_one_name = self.class.to_s.underscore # Update Obj Association if inverse klass_obj.send("#{klass_one_name}=", self) klass_obj.save! end save! klass_obj end # # ================================== # # Destroy All # # ================================== # define_method("#{class_name}_destroy_all") do # send(class_name).each(&:destroy!) if inverse # end # class_variable_set(:@@relationships, @relationships) # true end |