Module: SimplyStored::Couch::HasMany
- Included in:
- ClassMethods
- Defined in:
- lib/simply_stored/couch/has_many.rb
Defined Under Namespace
Classes: Property
Instance Method Summary collapse
- #define_has_many_count(name, through = nil) ⇒ Object
- #define_has_many_getter(name) ⇒ Object
- #define_has_many_setter_add(name) ⇒ Object
- #define_has_many_setter_remove(name) ⇒ Object
- #define_has_many_setter_remove_all(name) ⇒ Object
- #define_has_many_through_getter(name, through) ⇒ Object
- #has_many(name, options = {}) ⇒ Object
Instance Method Details
#define_has_many_count(name, through = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/simply_stored/couch/has_many.rb', line 96 def define_has_many_count(name, through = nil) method_name = name.to_s.singularize.underscore + "_count" define_method(method_name) do |*args| = args.first && args.first.is_a?(Hash) && args.first if .assert_valid_keys(:force_reload, :with_deleted) forced_reload = [:force_reload] with_deleted = [:with_deleted] else forced_reload = false with_deleted = false end if forced_reload || instance_variable_get("@#{method_name}").nil? instance_variable_set("@#{method_name}", count_associated(through || name, self.class, :with_deleted => with_deleted)) end instance_variable_get("@#{method_name}") end end |
#define_has_many_getter(name) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/simply_stored/couch/has_many.rb', line 8 def define_has_many_getter(name) define_method(name) do |*args| = args.first && args.first.is_a?(Hash) && args.first if .assert_valid_keys(:force_reload, :with_deleted) forced_reload = [:force_reload] with_deleted = [:with_deleted] else forced_reload = false with_deleted = false end if forced_reload || instance_variable_get("@#{name}").nil? instance_variable_set("@#{name}", find_associated(name, self.class, :with_deleted => with_deleted)) end instance_variable_get("@#{name}") end end |
#define_has_many_setter_add(name) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/simply_stored/couch/has_many.rb', line 56 def define_has_many_setter_add(name) define_method("add_#{name.to_s.singularize}") do |value| klass = self.class.get_class_from_name(name) raise ArgumentError, "expected #{klass} got #{value.class}" unless value.is_a?(klass) value.send("#{self.class.foreign_key}=", id) value.save cached_version = instance_variable_get("@#{name}") || [] instance_variable_set("@#{name}", cached_version << value) end end |
#define_has_many_setter_remove(name) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/simply_stored/couch/has_many.rb', line 68 def define_has_many_setter_remove(name) define_method "remove_#{name.to_s.singularize}" do |value| klass = self.class.get_class_from_name(name) raise ArgumentError, "expected #{klass} got #{value.class}" unless value.is_a?(klass) raise ArgumentError, "cannot remove not mine" unless value.send(self.class.foreign_key.to_sym) == id if self.class._find_property(name).[:dependent] == :destroy value.destroy else value.send("#{self.class.foreign_key}=", nil) value.save end cached_version = instance_variable_get("@#{name}") || [] instance_variable_set("@#{name}", cached_version.delete_if{|item| item.id == value.id}) end end |
#define_has_many_setter_remove_all(name) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/simply_stored/couch/has_many.rb', line 86 def define_has_many_setter_remove_all(name) define_method "remove_all_#{name}" do all = send("#{name}", :force_reload => true) all.collect{|i| i}.each do |item| send("remove_#{name.to_s.singularize}", item) end end end |
#define_has_many_through_getter(name, through) ⇒ Object
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 |
# File 'lib/simply_stored/couch/has_many.rb', line 27 def define_has_many_through_getter(name, through) raise ArgumentError, "no such relation: #{self} - #{through}" unless instance_methods.map(&:to_sym).include?(through.to_sym) define_method(name) do |*args| = args.first && args.first.is_a?(Hash) && args.first if .assert_valid_keys(:force_reload, :with_deleted) forced_reload = [:force_reload] with_deleted = [:with_deleted] else forced_reload = false with_deleted = false end if forced_reload || instance_variable_get("@#{name}").nil? # there is probably a faster way to query this intermediate_objects = find_associated(through, self.class, :with_deleted => with_deleted) through_objects = intermediate_objects.map do |intermediate_object| intermediate_object.send(name.to_s.singularize.underscore, :with_deleted => with_deleted) end.flatten.uniq instance_variable_set("@#{name}", through_objects) end instance_variable_get("@#{name}") end end |
#has_many(name, options = {}) ⇒ Object
4 5 6 |
# File 'lib/simply_stored/couch/has_many.rb', line 4 def has_many(name, = {}) property name, .merge(:class => SimplyStored::Couch::HasMany::Property) end |