Module: Startback::Support::DataObject

Included in:
Model, World
Defined in:
lib/startback/support/data_object.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &bl) ⇒ Object



12
13
14
15
16
17
# File 'lib/startback/support/data_object.rb', line 12

def method_missing(name, *args, &bl)
  return super unless args.empty? && bl.nil?
  return super unless pair = _data_key_for(name)

  pair.last ? !!@_data[pair.first] : @_data[pair.first]
end

Instance Method Details

#[](name) ⇒ Object



19
20
21
22
23
# File 'lib/startback/support/data_object.rb', line 19

def [](name)
  return nil unless pair = _data_key_for(name, false, false)

  @_data[pair.first]
end

#initialize(data = {}) ⇒ Object



5
6
7
# File 'lib/startback/support/data_object.rb', line 5

def initialize(data = {})
  @_data = data.dup.freeze
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/startback/support/data_object.rb', line 25

def respond_to?(name)
  super || !_data_key_for(name).nil?
end

#to_dataObject Also known as: to_h



29
30
31
# File 'lib/startback/support/data_object.rb', line 29

def to_data
  @_data
end

#to_json(*args, &bl) ⇒ Object



34
35
36
# File 'lib/startback/support/data_object.rb', line 34

def to_json(*args, &bl)
  to_data.to_json(*args, &bl)
end