Module: Backyard::Session

Defined in:
lib/backyard/session.rb

Instance Method Summary collapse

Instance Method Details

#get_model(model_type, name) ⇒ Object?

Retrieve a stored object from the backyard.

Parameters:

  • model_type (Symbol)
  • name (String)

Returns:

  • (Object, nil)


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.

Parameters:

  • type (Symbol)

Returns:

  • (Array<Object>)


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.

Parameters:

  • model_type (Symbol)
  • name (String)

Returns:

  • (Object)

See Also:



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.

Parameters:

  • model_type (Symbol)
  • name (String)

Returns:

  • (true, false)


45
46
47
# File 'lib/backyard/session.rb', line 45

def model_exists?(model_type, name)
  get_model(model_type, name) != nil
end

#model_storeObject



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.

Overloads:

  • #put_model(type, name, attributes) ⇒ Object

    creates an instance from type ‘type’ and saved it under the passed name. The attributes are forwarded to the object creation

    Parameters:

    • type (Symbol)
    • name (String)
    • attributes (Hash)

      additional parameters for the object creation

  • #put_model(object, name) ⇒ Object

    stores the given object under ‘name’

    Parameters:

    • object (Object)

      the object to store

    • name (String)

Returns:

  • (Object)

    the object, which was stored



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