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

Methods included from Log::Mixin

included, #logger, #logger=

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.

Parameters:

  • data (#each_pair)

Returns:

  • (self)

Raises:



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

#metadataNRSER::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.has_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