Class: Operatic::Data
- Inherits:
-
Object
- Object
- Operatic::Data
- Defined in:
- lib/operatic/data.rb
Class Method Summary collapse
-
.define(*attrs) ⇒ Object
Generate a subclass of Data with named
attrsaccessors.
Instance Method Summary collapse
-
#[](key) ⇒ anything
Return the value for
key. -
#[]=(key, value) ⇒ Object
Set data on the result.
- #freeze ⇒ self
-
#initialize(**kwargs) ⇒ Data
constructor
A new instance of Data.
- #merge(hash) ⇒ Data
- #to_h ⇒ Hash<Symbol, anything>
Constructor Details
#initialize(**kwargs) ⇒ Data
Returns a new instance of Data.
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.
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.
32 33 34 |
# File 'lib/operatic/data.rb', line 32 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
Set data on the result.
40 41 42 |
# File 'lib/operatic/data.rb', line 40 def []=(key, value) @data[key] = value end |
#freeze ⇒ self
45 46 47 48 |
# File 'lib/operatic/data.rb', line 45 def freeze @data.freeze super end |
#merge(hash) ⇒ Data
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_h ⇒ Hash<Symbol, anything>
61 62 63 |
# File 'lib/operatic/data.rb', line 61 def to_h @data end |