Module: Ambry::Model::ClassMethods

Extended by:
Forwardable
Defined in:
lib/ambry/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribute_namesObject

Returns the value of attribute attribute_names


17
18
19
# File 'lib/ambry/model.rb', line 17

def attribute_names
  @attribute_names
end

#id_methodObject

Returns the value of attribute id_method


17
18
19
# File 'lib/ambry/model.rb', line 17

def id_method
  @id_method
end

#key_classObject (readonly)

Returns the value of attribute key_class


18
19
20
# File 'lib/ambry/model.rb', line 18

def key_class
  @key_class
end

#mapperObject

Returns the value of attribute mapper


17
18
19
# File 'lib/ambry/model.rb', line 17

def mapper
  @mapper
end

Instance Method Details

#create(hash) ⇒ Object


56
57
58
# File 'lib/ambry/model.rb', line 56

def create(hash)
  new(hash).save
end

#field(*names) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ambry/model.rb', line 23

def field(*names)
  names.each do |name|
    # First attribute added is the default id
    id_field name if attribute_names.empty?
    attribute_names << name.to_sym
    class_eval(<<-EOM, __FILE__, __LINE__ + 1)
      def #{name}
        defined?(@#{name}) ? @#{name} : @attributes[:#{name}]
      end

      def #{name}=(value)
        @#{name} = value
      end
    EOM
  end
end

#filters(&block) ⇒ Object


74
75
76
77
78
79
80
81
82
83
# File 'lib/ambry/model.rb', line 74

def filters(&block)
  key_class.class_eval(&block)
  key_class.instance_methods(false).each do |name|
  instance_eval(<<-EOM, __FILE__, __LINE__ + 1)
    def #{name}(*args)
      mapper.key_set.#{name}(*args)
    end
  EOM
  end
end

#inspectObject


93
94
95
# File 'lib/ambry/model.rb', line 93

def inspect
  "#{name}(#{attribute_names * ', '})"
end

#use(adapter_name, options = {}) ⇒ Object


40
41
42
43
44
# File 'lib/ambry/model.rb', line 40

def use(adapter_name, options = {})
  @mapper         = nil
  @adapter_name   = adapter_name
  @mapper_options = options
end

#with_index(name = nil, &block) ⇒ Object

Memoize the output of the method call invoked in the block.

Parameters:

  • name (#to_s) (defaults to: nil)

    If not given, the name of the method calling with_index will be used.


48
49
50
51
52
53
54
# File 'lib/ambry/model.rb', line 48

def with_index(name = nil, &block)
  name ||= caller(1)[0].match(/in `(.*)'\z/)[1]
  mapper.indexes[name.to_s] or begin
    indexable = yield
    mapper.add_index(name, indexable)
  end
end