Module: Makura::Model::SingletonMethods
- Defined in:
- lib/makura/model.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#makura_relation ⇒ Object
Returns the value of attribute makura_relation.
-
#property_type ⇒ Object
Returns the value of attribute property_type.
Instance Method Summary collapse
- #[](id, rev = nil) ⇒ Object
- #belongs_to(name, model = nil) ⇒ Object
- #convert_raw(rows, flat = false) ⇒ Object
- #database ⇒ Object
- #database=(name) ⇒ Object
- #design ⇒ Object
- #has_many(name, model = nil) ⇒ Object
- #id(name) ⇒ Object
- #layout(name, opts = {}) ⇒ Object
-
#multi_fetch(name, opts = {}) ⇒ Object
opts
must include a :keys or ‘keys’ key with something that responds to #to_a as value. - #multi_fetch_with_docs(name, opts = {}) ⇒ Object (also: #multi_document_fetch)
- #plugin(name) ⇒ Object
- #properties(*names) ⇒ Object
- #property(name, opts = {}) ⇒ Object
- #proto_layout(common, name, opts = {}) ⇒ Object
- #save ⇒ Object
- #view(name, opts = {}) ⇒ Object
-
#view_with_docs(name, opts = {}) ⇒ Object
(also: #view_docs)
It is generally recommended not to include the doc in the emit of the map function but to use include_docs=true.
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
155 156 157 |
# File 'lib/makura/model.rb', line 155 def defaults @defaults end |
#makura_relation ⇒ Object
Returns the value of attribute makura_relation.
155 156 157 |
# File 'lib/makura/model.rb', line 155 def makura_relation @makura_relation end |
#property_type ⇒ Object
Returns the value of attribute property_type.
155 156 157 |
# File 'lib/makura/model.rb', line 155 def property_type @property_type end |
Instance Method Details
#[](id, rev = nil) ⇒ Object
242 243 244 245 246 |
# File 'lib/makura/model.rb', line 242 def [](id, rev = nil) new(database[id, rev]) rescue Error::ResourceNotFound nil end |
#belongs_to(name, model = nil) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/makura/model.rb', line 209 def belongs_to(name, model = nil) name = name.to_s klass = (model || name.capitalize).to_s @makura_relation[:belongs_to][name] = klass class_eval(" def #{name}() @#{name} ||= #{klass}[self[#{name.dump}]] end def #{name}=(obj) if obj.respond_to?(:_id) @_hash[#{name.dump}] = obj._id else @_hash[#{name.dump}] = obj end end") end |
#convert_raw(rows, flat = false) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/makura/model.rb', line 353 def convert_raw(rows, flat = false) rows.map do |row| value = row['doc'] || row['value'] if value.respond_to?(:to_hash) if type = value['type'] and not flat const_get(type).new(value) else row end elsif not row['key'] value else row end end end |
#database ⇒ Object
171 172 173 |
# File 'lib/makura/model.rb', line 171 def database @database || Makura::Model.database end |
#database=(name) ⇒ Object
167 168 169 |
# File 'lib/makura/model.rb', line 167 def database=(name) @database = Makura::Model.server.database(name) end |
#design ⇒ Object
248 249 250 |
# File 'lib/makura/model.rb', line 248 def design @design ||= Design.new(name.to_s, database) end |
#has_many(name, model = nil) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/makura/model.rb', line 227 def has_many(name, model = nil) name = name.to_s klass = (model || name.capitalize).to_s @makura_relation[:has_many][name] = klass class_eval(" def #{name}() @#{name} ||= #{klass}[self[#{name.dump}]] end def #{name}=(obj) return unless obj raise RuntimeError, 'You many not assign here' end") end |
#id(name) ⇒ Object
202 203 204 205 206 207 |
# File 'lib/makura/model.rb', line 202 def id(name) @id = name class_eval(" alias #{name} _id alias #{name}= _id=") end |
#layout(name, opts = {}) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/makura/model.rb', line 252 def layout(name, opts = {}) design[name] = layout = Layout.new(name, design) map_name = opts[:map] || "#{self.name}_#{name}".downcase reduce_name = opts[:reduce] || "#{self.name}_#{name}".downcase layout.load_map(map_name) layout.load_reduce(reduce_name) return layout end |
#multi_fetch(name, opts = {}) ⇒ Object
opts
must include a :keys or ‘keys’ key with something that responds to #to_a as value
Usage given a map named ‘Post/by_tags’ that does something like:
for(t in doc.tags){ emit([doc.tags[t]], null); }
You can use this like:
keys = ['ruby', 'couchdb']
Post.multi_fetch(:by_tags, :keys => keys)
And it will return all docs with the tags ‘ruby’ OR ‘couchdb’ This can be extended to match even more complex things
for(t in doc.tags){ emit([doc.author, doc.tags[t]], null); }
Now we do
keys = [['manveru', 'ruby'], ['mika', 'couchdb']]
Post.multi_fetch(:by_tags, :keys => keys)
This will return all docs match following:
(( == 'manveru' && .include?('ruby')) ||
( == 'mika' && .include?('couchdb')))
Of course you can add as many keys as you like:
keys = [['manveru', 'ruby'],
['manveru', 'couchdb'],
['mika', 'design']]
['mika', 'couchdb']]
Post.multi_fetch(:by_tags, :keys => keys)
From wiki.apache.org/couchdb/HTTP_view_API
A JSON structure of {"keys": ["key1", "key2", ...]} can be posted to
any user defined view or _all_docs to retrieve just the view rows
matching that set of keys. Rows are returned in the order of the keys
specified. Combining this feature with include_docs=true results in
the so-called multi-document-fetch feature.
322 323 324 325 326 327 |
# File 'lib/makura/model.rb', line 322 def multi_fetch(name, opts = {}) keys = opts.delete(:keys) || opts.delete('keys') opts.merge!(:payload => {'keys' => keys.to_a}) hash = database.post("_view/#{self}/#{name}", opts) convert_raw(hash['rows']) end |
#multi_fetch_with_docs(name, opts = {}) ⇒ Object Also known as: multi_document_fetch
329 330 331 332 |
# File 'lib/makura/model.rb', line 329 def multi_fetch_with_docs(name, opts = {}) opts.merge!(:include_docs => true, :reduce => false) multi_fetch(name, opts) end |
#plugin(name) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/makura/model.rb', line 157 def plugin(name) require "makura/plugin/#{name}".downcase name = name.to_s.capitalize mod = Makura::Plugin.const_get(name) include(mod::InstanceMethods) if defined?(mod::InstanceMethods) extend(mod::SingletonMethods) if defined?(mod::SingletonMethods) end |
#properties(*names) ⇒ Object
175 176 177 |
# File 'lib/makura/model.rb', line 175 def properties(*names) names.each{|name| property(name) } end |
#property(name, opts = {}) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/makura/model.rb', line 179 def property(name, opts = {}) name = name.to_s defaults[name] = default = opts.delete(:default) if opts[:default] property_type[name] = type = opts.delete(:type) if opts[:type] if type == Time code = " def #{name}() Time.at(@_hash[#{name.dump}].to_i) end def #{name}=(obj) @_hash[#{name.dump}] = obj.to_i end" class_eval(code) else code = " def #{name}() @_hash[#{name.dump}] end def #{name}=(obj) @_hash[#{name.dump}] = obj end" end class_eval(code) end |
#proto_layout(common, name, opts = {}) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/makura/model.rb', line 264 def proto_layout(common, name, opts = {}) design[name] = layout = Layout.new(name, design) map_name = opts.delete(:map) || "#{self.name}_#{common}".downcase reduce_name = opts.delete(:reduce) || "#{self.name}_#{common}".downcase layout.load_proto_map(map_name, opts) layout.load_proto_reduce(reduce_name, opts) return layout end |
#save ⇒ Object
276 277 278 |
# File 'lib/makura/model.rb', line 276 def save design.save end |
#view(name, opts = {}) ⇒ Object
346 347 348 349 350 351 |
# File 'lib/makura/model.rb', line 346 def view(name, opts = {}) flat = opts.delete(:flat) hash = database.view("#{self}/#{name}", opts) convert_raw(hash['rows'], flat) end |
#view_with_docs(name, opts = {}) ⇒ Object Also known as: view_docs
It is generally recommended not to include the doc in the emit of the map function but to use include_docs=true. To make using this approach more convenient use this method.
339 340 341 342 |
# File 'lib/makura/model.rb', line 339 def view_with_docs(name, opts = {}) opts.merge!(:include_docs => true, :reduce => false) view(name, opts) end |