Module: Backyard::Session
- Defined in:
- lib/backyard/session.rb
Instance Method Summary collapse
-
#get_model(model_type, name) ⇒ Object?
Retrieve a stored object from the backyard.
-
#get_models(model_type) ⇒ Array<Object>
Retrieve all objects for a given type.
-
#model(model_type, name, attributes = {}) ⇒ Object
This method is used when you don’t care if you get an existing instance or if a new one is beeing created.
-
#model_exists?(model_type, name) ⇒ true, false
Check if a model is stored in the backyard.
- #model_store ⇒ Object
-
#put_model(model_type, name = nil, attributes = {}) ⇒ Object
Put a model in the backyard.
Instance Method Details
#get_model(model_type, name) ⇒ Object?
Retrieve a stored object from the backyard.
34 35 36 37 38 |
# File 'lib/backyard/session.rb', line 34 def get_model(model_type, name) klass = class_for_type(model_type) result = model_store.get(klass, name) || Backyard.global_store.model_store.get(klass, name) reload_model(result) end |
#get_models(model_type) ⇒ Array<Object>
Retrieve all objects for a given type.
70 71 72 |
# File 'lib/backyard/session.rb', line 70 def get_models(model_type) model_store.get_collection class_for_type(model_type) end |
#model(model_type, name, attributes = {}) ⇒ Object
This method is used when you don’t care if you get an existing instance or if a new one is beeing created.
58 59 60 61 62 63 64 |
# File 'lib/backyard/session.rb', line 58 def model(model_type, name, attributes = {}) if model_exists?(model_type, name) get_model(model_type, name) else put_model(model_type, name, attributes) end end |
#model_exists?(model_type, name) ⇒ true, false
Check if a model is stored in the backyard.
45 46 47 |
# File 'lib/backyard/session.rb', line 45 def model_exists?(model_type, name) get_model(model_type, name) != nil end |
#model_store ⇒ Object
74 75 76 |
# File 'lib/backyard/session.rb', line 74 def model_store @store ||= ModelStore.new end |
#put_model(type, name, attributes) ⇒ Object #put_model(object, name) ⇒ Object
Put a model in the backyard.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/backyard/session.rb', line 17 def put_model(model_type, name = nil, attributes = {}) model_name = name.nil? ? adapter.generate_model_name(model_type) : name obj = if model_type.is_a?(String) || model_type.is_a?(Symbol) klass = class_for_type(model_type) attributes = apply_model_config(klass, model_name).merge(attributes) adapter.create(model_type, attributes) else model_type end model_store.put(model_name, obj) end |