Module: NRSER::Meta::Props
- Included in:
- Base
- Defined in:
- lib/nrser/meta/props.rb,
lib/nrser/meta/props/base.rb,
lib/nrser/meta/props/prop.rb
Defined Under Namespace
Modules: ClassMethods Classes: Base, Prop
Constant Summary collapse
- CLASS_KEY =
'__class__'- PROPS_VARIABLE_NAME =
:@__NRSER_props- PROP_VALUES_VARIABLE_NAME =
:@__NRSER_prop_values
Class Method Summary collapse
-
.from_data(data) ⇒ Object
Instantiate a class from a data hash.
- .get_props_ref(klass) ⇒ return_type
-
.included(base) ⇒ Object
Hook to extend the including class with NRSER::Meta::Props:ClassMethods.
Instance Method Summary collapse
- #initialize_props(values) ⇒ return_type
- #merge(overrides = {}) ⇒ Object
-
#to_data(only_own: false, only_primary: false, add_class: true) ⇒ Hash<String, Object>
Create a “data” representation suitable for transport, storage, etc.
- #to_h(only_own: false, only_primary: false) ⇒ Hash<Symbol, Object>
- #to_json(*args) ⇒ return_type
- #to_yaml(*args) ⇒ Object
Class Method Details
.from_data(data) ⇒ Object
Instantiate a class from a data hash. The hash must contain the ‘__class__` key and the target class must be loaded already.
WARNING
I’m sure this is all-sorts of unsafe. Please don’t ever think this is reasonable to use on untrusted data.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/nrser/meta/props.rb', line 60 def self.from_data data t.hash_.check data unless data.key?(CLASS_KEY) raise ArgumentError.new <<-END.dedent Data is missing #{ CLASS_KEY } key - no idea what class to instantiate. #{ data.pretty_inspect } END end class_name = t.str.check data[CLASS_KEY] klass = class_name.to_const klass.from_data data end |
.get_props_ref(klass) ⇒ return_type
Document get_props_ref method.
Returns @todo Document return value.
36 37 38 39 40 41 42 |
# File 'lib/nrser/meta/props.rb', line 36 def self.get_props_ref klass unless klass.instance_variable_defined? PROPS_VARIABLE_NAME klass.instance_variable_set PROPS_VARIABLE_NAME, {} end klass.instance_variable_get PROPS_VARIABLE_NAME end |
.included(base) ⇒ Object
Hook to extend the including class with NRSER::Meta::Props:ClassMethods
78 79 80 |
# File 'lib/nrser/meta/props.rb', line 78 def self.included base base.extend ClassMethods end |
Instance Method Details
#initialize_props(values) ⇒ return_type
Document initialize_props method.
Returns @todo Document return value.
188 189 190 191 192 |
# File 'lib/nrser/meta/props.rb', line 188 def initialize_props values self.class.props(only_primary: true).each { |name, prop| prop.set_from_values_hash self, values } end |
#merge(overrides = {}) ⇒ Object
195 196 197 198 199 |
# File 'lib/nrser/meta/props.rb', line 195 def merge overrides = {} self.class.new( self.to_h(only_primary: true).merge(overrides.symbolize_keys) ) end |
#to_data(only_own: false, only_primary: false, add_class: true) ⇒ Hash<String, Object>
Create a “data” representation suitable for transport, storage, etc.
The result is meant to consist of only basic data types and structures - strings, numbers, arrays, hashes, datetimes, etc… though it depends on any custom objects it encounters correctly responding to ‘#to_data` for this to happen (as is implemented from classes that mix in Props here).
Prop names are converted to strings (from symbols) since though YAML supports symbol values, they have poor portability across languages, and they mean the same thing in this situation.
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/nrser/meta/props.rb', line 232 def to_data only_own: false, only_primary: false, add_class: true self.class.props(only_own: false, only_primary: false). map { |name, prop| [name.to_s, prop.to_data(self)] }. to_h. tap { |hash| hash[CLASS_KEY] = self.class.name if add_class } end |
#to_h(only_own: false, only_primary: false) ⇒ Hash<Symbol, Object>
Document to_h method.
Returns @todo Document return value.
210 211 212 213 |
# File 'lib/nrser/meta/props.rb', line 210 def to_h only_own: false, only_primary: false self.class.props(only_own: only_own, only_primary: only_primary). map_values { |name, prop| prop.get self } end |
#to_json(*args) ⇒ return_type
Document to_json method.
Returns @todo Document return value.
252 253 254 |
# File 'lib/nrser/meta/props.rb', line 252 def to_json *args to_data.to_json *args end |
#to_yaml(*args) ⇒ Object
257 258 259 |
# File 'lib/nrser/meta/props.rb', line 257 def to_yaml *args to_data.to_yaml *args end |