Module: NRSER::Props::InstanceMethods

Defined in:
lib/nrser/props/instance_methods.rb

Overview

Instance methods to mix in to classes that include NRSER::Props.

Instance Method Summary collapse

Instance Method Details

#dupObject



82
83
84
85
# File 'lib/nrser/props/instance_methods.rb', line 82

def dup
  self.class.new \
    self.to_h( only_primary: true )
end

#merge(other, &block) ⇒ Object

Parameters:

  • block (Proc<(KEY, CURRENT, UPDATE) => VALUE>)

    Optional block to handle conflicts.



75
76
77
78
79
# File 'lib/nrser/props/instance_methods.rb', line 75

def merge other, &block
  self.class.new \
    self.to_h( only_primary: true ).
    merge( other.symbolize_keys, &block )
end

#to_data(only_own: false, only_primary: false, add_class: true, class_key: NRSER::Props::DEFAULT_CLASS_KEY, compact: true) ⇒ Hash<String, *>

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:

  • only_own (Boolean) (defaults to: false)

    When ‘true`, don’t include parent properties.

  • only_primary (Boolean) (defaults to: false)

    When ‘true`, don’t include sourced properties.

  • add_class (Boolean) (defaults to: true)

    Add a special key with the class’ name as the value.

  • class_key (String) (defaults to: NRSER::Props::DEFAULT_CLASS_KEY)

    Name for special class key.

Returns:

  • (Hash<String, *>)

    Map of property names as strings to their “data” value, plus the special class identifier key and value, if requested.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/nrser/props/instance_methods.rb', line 137

def to_data only_own: false,
            only_primary: false,
            add_class: true,
            class_key: NRSER::Props::DEFAULT_CLASS_KEY,
            compact: true
            
  hash = self.class.props(only_own: only_own, only_primary: only_primary).
    map { |name, prop|
      [name.to_s, prop.to_data(self)]
    }.
    to_h
  
  hash.compact! if compact
  hash[class_key] = self.class.safe_name if add_class
  
  hash
end

#to_h(only_own: false, only_primary: false, compact: true) ⇒ Hash<Symbol, Object>

Create a new hash with property names mapped to values.

Parameters:

  • only_own (Boolean) (defaults to: false)

    When ‘true`, don’t include parent properties.

  • only_primary (Boolean) (defaults to: false)

    When ‘true`, don’t include sourced properties.

Returns:



99
100
101
102
103
104
105
106
107
# File 'lib/nrser/props/instance_methods.rb', line 99

def to_h only_own: false, only_primary: false, compact: true
  hash = self.class.
    props(only_own: only_own, only_primary: only_primary).
    transform_values { |prop| prop.get self }
  
  hash.compact! if compact
  
  hash
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:



167
168
169
# File 'lib/nrser/props/instance_methods.rb', line 167

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:



179
180
181
# File 'lib/nrser/props/instance_methods.rb', line 179

def to_yaml *args
  to_data.to_yaml *args
end