module NRSER::Props::InstanceMethods

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

Public Instance Methods

dup() click to toggle source
# File lib/nrser/props/instance_methods.rb, line 82
def dup
  self.class.new \
    self.to_h( only_primary: true )
end
merge(other, &block) click to toggle source

@param [Proc<(KEY, CURRENT, UPDATE) => VALUE>] block

Optional block to handle conflicts.
# 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) click to toggle source

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.

@param [Boolean] only_own

When `true`, don't include parent properties.

@param [Boolean] only_primary

When `true`, don't include sourced properties.

@param [Boolean] add_class

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

@param [String] class_key

Name for special class key.

@return [Hash<String, *>]

Map of property names as strings to their "data" value, plus the special
class identifier key and value, if requested.
# 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) click to toggle source

Create a new hash with property names mapped to values.

@param [Boolean] only_own

When `true`, don't include parent properties.

@param [Boolean] only_primary

When `true`, don't include sourced properties.

@return [Hash<Symbol, Object>]

Map of prop names to values.
# 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) click to toggle source

Get a JSON {String} encoding the instance's data.

@param [Array] args

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

@return [String]

# File lib/nrser/props/instance_methods.rb, line 167
def to_json *args
  to_data.to_json *args
end
to_yaml(*args) click to toggle source

Get a YAML {String} encoding the instance's data.

@param [Array] args

I really don't know... whatever {YAML.dump} sends to it i guess.

@return [String]

# File lib/nrser/props/instance_methods.rb, line 179
def to_yaml *args
  to_data.to_yaml *args
end