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

DEFAULT_CLASS_KEY =
'__class__'
PROPS_VARIABLE_NAME =
:@__NRSER_props
PROP_VALUES_VARIABLE_NAME =
:@__NRSER_prop_values

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_props_ref(klass) ⇒ return_type

TODO:

Document get_props_ref method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



37
38
39
40
41
42
43
# File 'lib/nrser/meta/props.rb', line 37

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



119
120
121
# File 'lib/nrser/meta/props.rb', line 119

def self.included base
  base.extend ClassMethods
end

.UNSAFE_load_instance_from_data(data, class_key: DEFAULT_CLASS_KEY) ⇒ NRSER::Meta::Props

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.

Parameters:

  • data (Hash<String, Object>)

    Data hash to load from.

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
# File 'lib/nrser/meta/props.rb', line 62

def self.UNSAFE_load_instance_from_data data, class_key: DEFAULT_CLASS_KEY
  t.hash_.check data
  
  unless data.key?( class_key )
    raise ArgumentError.new binding.erb "      Data is missing <%= class_key %> key - no idea what class to \n      instantiate.\n      \n      Data:\n      \n          <%= data.pretty_inspect %>\n      \n    ERB\n  end\n  \n  # Get the class name from the data hash using the key, checking that it's\n  # a non-empty string.\n  class_name = t.non_empty_str.check data[class_key]\n  \n  # Resolve the constant at that name.\n  klass = class_name.to_const\n  \n  # Make sure it's one of ours\n  unless klass.included_modules.include?( NRSER::Meta::Props )\n    raise ArgumentError.new binding.erb <<-ERB\n      Can not load instance from data - bad class name.\n      \n      Extracted class name\n      \n          <%= class_name.inspect %>\n      \n      from class key\n      \n          <%= class_key.inspect %>\n      \n      which resolved to constant\n      \n          <%= klass.inspect %>\n      \n      but that class does not include the NRSER::Meta::Props mixin, which we\n      check for to help protect against executing an unrelated `.from_data`\n      class method when attempting to load.\n      \n      Data:\n      \n          <%= data.pretty_inspect %>\n      \n    ERB\n  end\n  \n  # Kick off the restore and return the result\n  klass.from_data data\n  \nend\n"

Instance Method Details

#initialize_props(values) ⇒ return_type

TODO:

Document initialize_props method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



255
256
257
258
259
# File 'lib/nrser/meta/props.rb', line 255

def initialize_props values
  self.class.props(only_primary: true).each { |name, prop|
    prop.set_from_values_hash self, values
  }
end

#merge(overrides = {}) ⇒ Object



262
263
264
265
266
# File 'lib/nrser/meta/props.rb', line 262

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, class_key: NRSER::Meta::Props::DEFAULT_CLASS_KEY) ⇒ 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.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (Hash<String, Object>)

    @todo Document return value.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/nrser/meta/props.rb', line 301

def to_data only_own: false,
            only_primary: false,
            add_class: true,
            class_key: NRSER::Meta::Props::DEFAULT_CLASS_KEY
            
  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>

TODO:

Document to_h method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (Hash<Symbol, Object>)

    @todo Document return value.



277
278
279
280
281
# File 'lib/nrser/meta/props.rb', line 277

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) ⇒ String

Get a JSON String encoding the instance’s data.

Parameters:

  • *args (Array)

    I really don’t know. ‘#to_json` takes at last one argument, but I’ve had trouble finding a spec for it :/

Returns:

  • (String)


328
329
330
# File 'lib/nrser/meta/props.rb', line 328

def to_json *args
  to_data.to_json *args
end

#to_yaml(*args) ⇒ String

Get a YAML String encoding the instance’s data.

Parameters:

  • *args (Array)

    I really don’t know… whatever YAML.dump sends to it i guess.

Returns:

  • (String)


340
341
342
# File 'lib/nrser/meta/props.rb', line 340

def to_yaml *args
  to_data.to_yaml *args
end