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:

  • type (Symbol)

    the type of the object to retrieve

  • name (String)

    the name of the object to retrieve

Returns:

  • (Object, nil)

    the object with the given type and name



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)
  reload_model(result)
end

#get_models(model_type) ⇒ Array<Object>

Retrieve all objects for a given type.

Parameters:

  • type (Symbol)

    the type of the objects to retrieve

Returns:

  • (Array<Object>)

    an array of objects of the 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.

Parameters:

  • model_type (Symbol)

    the type of the object

  • name (String)

    the name of the object

Returns:

  • (Object)

    an object with the given type and name

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:

  • type (Symbol)

    the type of the object

  • name (String)

    the name of the object

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)

      the type of object that should get created

    • name (String)

      the name for the newly created object

    • 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)

      the name for the object

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