Module: NRSER::Props::ClassMethods
- Includes:
- Log::Mixin
- Defined in:
- lib/nrser/props/class_methods.rb
Overview
Class method “macros” that are extended in to data classes, providing the declaration interface.
Instance Method Summary collapse
-
#from_data(data) ⇒ self
Instantiate from a data hash.
- #invariant(*args, &block) ⇒ Object
- #invariants(*args, &block) ⇒ Object
-
#metadata ⇒ NRSER::Props::Metadata
Get the metadata object for this class, creating it if it doesn’t exist.
- #prop(*args, &block) ⇒ Object
- #props(*args, &block) ⇒ Object
Methods included from Log::Mixin
Instance Method Details
#from_data(data) ⇒ self
TODO:
This needs to be extended to handle prop’d classes nested in arrays and hashes… but for the moment, it is what it is.
This may have been fixed…?
Instantiate from a data hash.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/nrser/props/class_methods.rb', line 90 def from_data data values = {} props = self.props unless data.respond_to? :each_pair raise NRSER::ArgumentError.new \ "`data` argument must respond to `#each_pair`", data: data, class: self end data.each_pair do |data_key, data_value| prop_key = case data_key when Symbol data_key when String data_key.to_sym end if prop_key && (prop = props[prop_key]) values[prop_key] = prop.value_from_data data_value end end self.new values end |
#invariant(*args, &block) ⇒ Object
70 71 72 |
# File 'lib/nrser/props/class_methods.rb', line 70 def invariant *args, &block .invariant *args, &block end |
#invariants(*args, &block) ⇒ Object
65 66 67 |
# File 'lib/nrser/props/class_methods.rb', line 65 def invariants *args, &block .invariants *args, &block end |
#metadata ⇒ NRSER::Props::Metadata
Get the metadata object for this class, creating it if it doesn’t exist.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/nrser/props/class_methods.rb', line 42 def # TODO Move into {NRSER::Props::Metadata}? # unless NRSER::Props::Metadata. self instance_variable_set \ NRSER::Props::Metadata::VARIABLE_NAME, NRSER::Props::Metadata.new( self ) end NRSER::Props::Metadata. self end |
#prop(*args, &block) ⇒ Object
60 61 62 |
# File 'lib/nrser/props/class_methods.rb', line 60 def prop *args, &block .prop *args, &block end |
#props(*args, &block) ⇒ Object
55 56 57 |
# File 'lib/nrser/props/class_methods.rb', line 55 def props *args, &block .props *args, &block end |