Class: Operatic::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/operatic/data.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Data

Returns a new instance of Data.

Parameters:

  • kwargs (Hash<Symbol, anything>)


23
24
25
# File 'lib/operatic/data.rb', line 23

def initialize(**kwargs)
  @data = kwargs
end

Class Method Details

.define(*attrs) ⇒ Object

Generate a subclass of Operatic::Data with named attrs accessors. This wouldn’t normally be called directly, see ClassMethods#data_attr for example usage.

Parameters:

  • attrs (Array<Symbol>)

    a list of convenience data accessors.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/operatic/data.rb', line 8

def self.define(*attrs)
  Class.new(self) do
    attrs.each do |name|
      define_method name do
        self[name]
      end

      define_method "#{name}=" do |value|
        self[name] = value
      end
    end
  end
end

Instance Method Details

#[](key) ⇒ anything

Return the value for key.

Parameters:

  • key (Symbol)

Returns:

  • (anything)


32
33
34
# File 'lib/operatic/data.rb', line 32

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object

Set data on the result.

Parameters:

  • key (Symbol)
  • value (anything)


40
41
42
# File 'lib/operatic/data.rb', line 40

def []=(key, value)
  @data[key] = value
end

#freezeself

Returns:

  • (self)


45
46
47
48
# File 'lib/operatic/data.rb', line 45

def freeze
  @data.freeze
  super
end

#merge(hash) ⇒ Data

Parameters:

  • hash (Hash<Symbol, anything>)

Returns:



53
54
55
56
57
58
# File 'lib/operatic/data.rb', line 53

def merge(hash)
  self.class.new.tap { |other|
    other.set_data(@data)
    other.set_data(hash)
  }
end

#to_hHash<Symbol, anything>

Returns:

  • (Hash<Symbol, anything>)


61
62
63
# File 'lib/operatic/data.rb', line 61

def to_h
  @data
end