Class: Garner::Mixins::Mongoid::Identity
- Inherits:
-
Object
- Object
- Garner::Mixins::Mongoid::Identity
- Includes:
- Cache::Binding
- Defined in:
- lib/garner/mixins/mongoid/identity.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#handle ⇒ Object
Returns the value of attribute handle.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#proxy_binding ⇒ Mongoid::Document
Return an object that can act as a binding on this identity’s behalf.
Class Method Summary collapse
- .conditions_for(klass, handle) ⇒ Object
-
.from_class_and_handle(klass, handle) ⇒ Garner::Mixins::Mongoid::Identity
Instantiate a new Mongoid::Identity.
- .validate_class!(klass) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Identity
constructor
A new instance of Identity.
-
#to_s ⇒ String
Stringize this identity for purposes of marshaling.
Methods included from Cache::Binding
#garner_cache_key, #invalidate_garner_caches, #invalidation_strategy, #key_strategy
Constructor Details
#initialize ⇒ Identity
Returns a new instance of Identity.
25 26 27 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 25 def initialize @conditions = {} end |
Instance Attribute Details
#conditions ⇒ Object
Returns the value of attribute conditions.
7 8 9 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 7 def conditions @conditions end |
#handle ⇒ Object
Returns the value of attribute handle.
7 8 9 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 7 def handle @handle end |
#klass ⇒ Object
Returns the value of attribute klass.
7 8 9 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 7 def klass @klass end |
#proxy_binding ⇒ Mongoid::Document
Return an object that can act as a binding on this identity’s behalf.
32 33 34 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 32 def proxy_binding @proxy_binding end |
Class Method Details
.conditions_for(klass, handle) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 52 def self.conditions_for(klass, handle) # Multiple-ID conditions conditions = { '$or' => Garner.config.mongoid_identity_fields.map do |field| { field => handle } end } # _type conditions selector = klass.where({}) conditions.merge!(selector.send(:type_selection)) if selector.send(:type_selectable?) conditions end |
.from_class_and_handle(klass, handle) ⇒ Garner::Mixins::Mongoid::Identity
Instantiate a new Mongoid::Identity.
15 16 17 18 19 20 21 22 23 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 15 def self.from_class_and_handle(klass, handle) validate_class!(klass) new.tap do |identity| identity.klass = klass identity.handle = handle identity.conditions = conditions_for(klass, handle) end end |
.validate_class!(klass) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 44 def self.validate_class!(klass) if !klass.include?(::Mongoid::Document) fail 'Must instantiate from a Mongoid class' elsif klass. fail 'Cannot instantiate from an embedded document class' end end |
Instance Method Details
#to_s ⇒ String
Stringize this identity for purposes of marshaling.
40 41 42 |
# File 'lib/garner/mixins/mongoid/identity.rb', line 40 def to_s "#{self.class.name}/klass=#{klass},handle=#{handle}" end |