Module: Cooler::Model::ClassMethods

Defined in:
lib/cooler/model.rb

Overview

Defines class methods for a Model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_valuesObject (readonly)

Gets the default values.



29
30
31
# File 'lib/cooler/model.rb', line 29

def default_values
  @default_values
end

Instance Method Details

#create(*attrs) ⇒ Object

Initializes a new instance, and saves it at the same time.

attrs - May contain at the first position, the String key for the

document owner of the instance. Then it a Hash with its
attributes (default: {}).

Returns the Object new instance.



123
124
125
126
127
# File 'lib/cooler/model.rb', line 123

def create(*attrs)
  instance = new(*attrs)
  instance.save
  instance
end

#default(attr, value, &block) ⇒ Object

Defines a default value for an attribute.

attr - The String or Symbol attribute name, to add the default value. value - The Object to use as default value (optional). block - A block to be run as default value (optional).

Examples

default :clicks, []
default :seconds, -> { Time.now.seconds }
default :app_key, lambda { |install| install.app.key }

Returns nothing. Raises NameError if attribute does not exist.



66
67
68
69
70
71
72
# File 'lib/cooler/model.rb', line 66

def default(attr, value, &block)
  unless instance_methods.include?("#{attr}=".to_sym) &&
    instance_methods.include?(attr.to_sym)
    raise NameError, "Unknown attribute #{attr}"
  end
  @default_values[attr.to_sym] = value || block
end

#get(key) ⇒ Object

Gets an Object from Couchbase, and construct an instance by its key. In order to use Hash version, the class should define a key.

key - The String that contains the key to query the object. Or the

Hash with the value defined in the key to search. See examples
for extra reference.

Examples

class Install
  key { |i| "install_#{i.app_key}" }
end

install = Install.get(app_key: '123')
# => Gets object with key: 'install_123'

install = Install.get('install_123')
# => Gets object with key: 'install_123'

Return a Model instance or nothing if not found. Raises NameError, if searching by key and key not defined.



95
96
97
98
99
100
101
# File 'lib/cooler/model.rb', line 95

def get(key)
  if Hash === key
    raise NameError, 'Key not defined' unless @key_block
    key = @key_block.(OpenStruct.new(key))
  end
  result = Cooler::Adapter.get.(key) and new(key, result)
end

#get!(key) ⇒ Object

Gets an Object from Couchbase, and construct an instance by its key. Raises an error if not found.

key - The String that contains the key to query the object.

Return a Model instance. Raises Cooler::NotFound if not found. Compatible with Sinatra::NotFound; www.sinatrarb.com/intro.html#Not%20Found



112
113
114
# File 'lib/cooler/model.rb', line 112

def get!(key)
  get(key) || raise(Cooler::NotFound)
end

#inherited(subclass) ⇒ Object

Called after the class is inherited. It will copy its default values, so subclass can use them too. For more informaiton, read: www.ruby-doc.org/core-1.9.3/Class.html#method-i-inherited

Returns nothing.



23
24
25
26
# File 'lib/cooler/model.rb', line 23

def inherited(subclass)
  subclass.instance_variable_set :@default_values, default_values.dup
  subclass.instance_variable_set :@key_block, @key_block.dup
end

#key(&block) ⇒ Object

Define the key for documents of this instance.

block - A block that contains how to build the key for an instance.

It receives the instance as an argument. This block should
return a String.

Examples

key { |click| ['click', click.app_key, click.tracking].join('_') }
key do |install|
  ['install', install.app_key, install.finger_print].join('_')
end

Returns nothing if passing a block, the block if not passing it.



45
46
47
48
49
50
# File 'lib/cooler/model.rb', line 45

def key(&block)
  if block_given?
    @key_block = block
  end
  @key_block || Proc.new { }
end