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

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 define_has_many_embedded_count(name, options, through = nil)
  method_name = name.to_s.singularize.underscore.gsub('/', '__') + "_count"
  define_method(method_name) do |*args|
    local_options = args.first && args.first.is_a?(Hash) && args.first
    if local_options
      local_options.assert_valid_keys(:force_reload, :with_deleted)
      forced_reload = local_options[:force_reload]
      with_deleted = local_options[: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 || options[:class_name], self.class, :with_deleted => with_deleted, :foreign_key => options[: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 define_has_many_embedded_getter(name, options)
  # 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?(options[:class_name].constantize)
        o = h
        o.parent_object = self
      else
        o = options[: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
    local_options = args.first && args.first.is_a?(Hash)
    forced_reload, with_deleted, limit, descending = extract_association_options(local_options)

    cached_results = send("_get_cached_#{name}") || {}
    cache_key = _cache_key_for(local_options)
    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 = options[:class_name].constantize.new; o._document = h; o}.map{|o| o.parent_object = self; o}
      instance_variable_set("@#{name}", cached_results)
      self.class.set_parent_has_many_embedded_association_object(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 define_has_many_embedded_setter(name, options)
  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 define_has_many_embedded_setter_add(name, options)
  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 define_has_many_embedded_setter_remove(name, options)
  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 define_has_many_embedded_setter_remove_all(name, options)
  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, options)
  define_method "reset_#{name}_index_values" do
    i = 0
    for embedded_document in (instance_variable_get("@#{name}") || [])
      embedded_document.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 has_many_embedded(name, options = {})
  check_existing_properties(name, SimplyCouch::Model::HasMany::Property)
  properties << SimplyCouch::Model::HasManyEmbedded::Property.new(self, name, options)
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 set_parent_has_many_embedded_association_object(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