Class: Norman::Mapper
- Inherits:
-
Object
- Object
- Norman::Mapper
- Extended by:
- Forwardable
- Defined in:
- lib/norman/mapper.rb
Overview
Mappers provide the middle ground between models and adapters. Mappers are responsible for performing finds and moving objects in and out of the hash.
Instance Attribute Summary collapse
-
#adapter_name ⇒ Object
Returns the value of attribute adapter_name.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#indexes ⇒ Object
Returns the value of attribute indexes.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns a hash or model attributes corresponding to the provided key.
-
#[]=(key, value) ⇒ Object
Sets a hash by key.
-
#adapter ⇒ Object
Get the adapter.
-
#add_index(name, indexable) ⇒ Object
Memoize the output of a find in a threadsafe manner.
-
#get(key) ⇒ Object
Get an instance by key.
-
#initialize(klass, adapter_name = nil, options = {}) ⇒ Mapper
constructor
A new instance of Mapper.
- #key_set ⇒ Object
-
#put(instance) ⇒ Object
Sets an instance, invoking its to_id method.
Constructor Details
#initialize(klass, adapter_name = nil, options = {}) ⇒ Mapper
Returns a new instance of Mapper.
13 14 15 16 17 18 19 20 |
# File 'lib/norman/mapper.rb', line 13 def initialize(klass, adapter_name = nil, = {}) @klass = klass @adapter_name = adapter_name || Norman.default_adapter_name @indexes = {} @lock = Mutex.new @options = @hash = adapter.db_for(klass) end |
Instance Attribute Details
#adapter_name ⇒ Object
Returns the value of attribute adapter_name.
9 10 11 |
# File 'lib/norman/mapper.rb', line 9 def adapter_name @adapter_name end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
8 9 10 |
# File 'lib/norman/mapper.rb', line 8 def hash @hash end |
#indexes ⇒ Object
Returns the value of attribute indexes.
9 10 11 |
# File 'lib/norman/mapper.rb', line 9 def indexes @indexes end |
#klass ⇒ Object
Returns the value of attribute klass.
9 10 11 |
# File 'lib/norman/mapper.rb', line 9 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/norman/mapper.rb', line 9 def @options end |
Instance Method Details
#[](key) ⇒ Object
Returns a hash or model attributes corresponding to the provided key.
23 24 25 |
# File 'lib/norman/mapper.rb', line 23 def [](key) hash[key] or raise NotFoundError.new(klass, key) end |
#[]=(key, value) ⇒ Object
Sets a hash by key.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/norman/mapper.rb', line 28 def []=(key, value) @lock.synchronize do @indexes = {} if value.id_changed? hash.delete value.to_id(true) end saved = hash[key] = value.to_hash.freeze adapter.save_database if @options[:sync] saved end end |
#adapter ⇒ Object
Get the adapter.
48 49 50 |
# File 'lib/norman/mapper.rb', line 48 def adapter Norman.adapters[adapter_name] end |
#add_index(name, indexable) ⇒ Object
Memoize the output of a find in a threadsafe manner.
41 42 43 44 45 |
# File 'lib/norman/mapper.rb', line 41 def add_index(name, indexable) @lock.synchronize do @indexes[name] = indexable end end |
#get(key) ⇒ Object
Get an instance by key
53 54 55 |
# File 'lib/norman/mapper.rb', line 53 def get(key) klass.send :from_hash, self[key] end |
#key_set ⇒ Object
57 58 59 |
# File 'lib/norman/mapper.rb', line 57 def key_set klass.key_class.new(hash.keys.freeze, self) end |
#put(instance) ⇒ Object
Sets an instance, invoking its to_id method
62 63 64 |
# File 'lib/norman/mapper.rb', line 62 def put(instance) self[instance.to_id] = instance end |