Module: Tardvig::DataIdentifier

Included in:
HashContainer
Defined in:
lib/tardvig/data_identifier.rb

Overview

Mixin for containers which adds them ability to use “data identifier”: universal selector of data which contains in a container. It is useful in situation when you need to access data, but you can not directly get them (e. g. in client-server games).

Instance Method Summary collapse

Instance Method Details

#get_this(id) ⇒ Object

Gets an element from this container using data identifier.

Parameters:

  • id (Symbol, String)

    key of the element. It supports attributes (id ‘:key` for `object.key`) and the `[]` method (`:key` for `object` or `object`). You can also get elements from container which is contained in this container (etc) using the `/` symbol (`foo/bar` for `object.foo.bar`).



13
14
15
16
17
18
19
# File 'lib/tardvig/data_identifier.rb', line 13

def get_this(id)
  obj = self
  id.to_s.split('/').each do |part|
    obj = get_elem_from_id obj, part
  end
  obj
end