Module: SimplyCouch::Model::HasManyEmbedded
- Included in:
- ClassMethods
- Defined in:
- lib/simply_couch/model/has_many_embedded.rb
Defined Under Namespace
Classes: Property
Instance Method Summary collapse
-
#define_has_many_embedded_count(name, options, through = nil) ⇒ Object
Not converted yet.
- #define_has_many_embedded_getter(name, options) ⇒ Object
- #define_has_many_embedded_setter(name, options) ⇒ Object
- #define_has_many_embedded_setter_add(name, options) ⇒ Object
- #define_has_many_embedded_setter_remove(name, options) ⇒ Object
-
#define_has_many_embedded_setter_remove_all(name, options) ⇒ Object
Not converted yet.
- #define_reset_index_values(name, options) ⇒ Object
- #has_many_embedded(name, options = {}) ⇒ Object
-
#set_parent_has_many_embedded_association_object(parent, child_collection) ⇒ Object
Not converted yet.
Instance Method Details
#define_has_many_embedded_count(name, options, through = nil) ⇒ Object
Not converted yet
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 129 def (name, , through = nil) method_name = name.to_s.singularize.underscore.gsub('/', '__') + "_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 || [:class_name], self.class, :with_deleted => with_deleted, :foreign_key => [:foreign_key])) end instance_variable_get("@#{method_name}") end end |
#define_has_many_embedded_getter(name, options) ⇒ Object
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 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 9 def (name, ) # Make an alias to the property getter. property_getter_method = "#{name}_property_getter" alias_method property_getter_method, name define_method(name) do |*args| current = instance_variable_get("@#{name}") return current if current && current.respond_to?(:parent_object_set?) # Rebuild current, ensure parent object current = Array.wrap(self.send(property_getter_method)).map do |h| if h.is_a?([:class_name].constantize) o = h o.parent_object = self else o = [:class_name].constantize.new o._document = h o.parent_object = self end o end def current.parent_object_set? true end instance_variable_set("@#{name}", current) return current = args.first && args.first.is_a?(Hash) forced_reload, with_deleted, limit, descending = () cached_results = send("_get_cached_#{name}") || {} cache_key = _cache_key_for() debugger if forced_reload || cached_results[cache_key].nil? #cached_results[cache_key] = get_embedded(options[:class_name], self.class, :with_deleted => with_deleted, :limit => limit, :descending => descending, :foreign_key => options[:foreign_key]) cached_results[cache_key] = Array.wrap(self.send(property_getter_method)).map{|h| o = [:class_name].constantize.new; o._document = h; o}.map{|o| o.parent_object = self; o} instance_variable_set("@#{name}", cached_results) self.class.(self, cached_results[cache_key]) end cached_results[cache_key] end end |
#define_has_many_embedded_setter(name, options) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 49 def (name, ) define_method("#{name}=") do |values| klass = self.class.get_class_from_name(name) raise ArgumentError, "expected Array got #{values.class}" unless values.is_a?(Array) instance_variable_set("@#{name}", []) unless instance_variable_get("@#{name}") iid = 0 for value in values if value.is_a?(Hash) newval = klass.new newval._document = value newval.updated_at ||= Time.now newval.created_at ||= Time.now else newval = value end newval.index = iid instance_variable_get("@#{name}") << newval iid += 1 end save end end |
#define_has_many_embedded_setter_add(name, options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 72 def (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) if value.is_a?(Hash) newval = klass.new newval._document = value newval.updated_at ||= Time.now newval.created_at ||= Time.now else newval = value end newval.index = (instance_variable_get("@#{name}") || []).size instance_variable_get("@#{name}") << newval save end end |
#define_has_many_embedded_setter_remove(name, options) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 90 def (name, ) define_method "remove_#{name.to_s.singularize}" do |value| klass = self.class.get_class_from_name(name) if value.is_a?(klass) found = instance_variable_get("@#{name}").delete(value) if found self.is_dirty self.send("reset_#{name}_index_values") end else raise ArgumentError, "expected #{klass} got #{value.class}" end return save end end |
#define_has_many_embedded_setter_remove_all(name, options) ⇒ Object
Not converted yet
118 119 120 121 122 123 124 125 126 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 118 def (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_reset_index_values(name, options) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 107 def define_reset_index_values(name, ) define_method "reset_#{name}_index_values" do i = 0 for in (instance_variable_get("@#{name}") || []) .index = i i += 1 end end end |
#has_many_embedded(name, options = {}) ⇒ Object
4 5 6 7 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 4 def (name, = {}) check_existing_properties(name, SimplyCouch::Model::HasMany::Property) properties << SimplyCouch::Model::HasManyEmbedded::Property.new(self, name, ) end |
#set_parent_has_many_embedded_association_object(parent, child_collection) ⇒ Object
Not converted yet
150 151 152 153 154 155 156 |
# File 'lib/simply_couch/model/has_many_embedded.rb', line 150 def (parent, child_collection) child_collection.each do |child| if child.respond_to?("#{parent.class.name.to_s.singularize.downcase}=") child.send("#{parent.class.name.to_s.singularize.camelize.downcase}=", parent) end end end |