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.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log::Mixin

included, #logger, #logger=

Class Method Details

.from(source) ⇒ self

Get an instance from a source.

Parameters:

Returns:

  • (self)


135
136
137
138
139
140
141
142
143
144
# File 'lib/nrser/props/class_methods.rb', line 135

def self.from source
  return source if source.is_a?( self )
  return from_s( source ) if source.is_a?( String )
  return from_data( source ) if source.respond_to?( :each_pair )
  return from_data( source.to_h ) if source.respond_to?( :to_h )
  
  raise NRSER::ArgumentError.new \
    "Unable to load #{ self } from source",
    source: source
end

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:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nrser/props/class_methods.rb', line 99

def from_data data
  values = {}
  
  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 = prop_for( prop_key, only_primary: true ))
      values[prop_key] = prop.value_from_data data_value
    end
  end
  
  self.new values
end

#invariant(*args, &block) ⇒ Object



79
80
81
# File 'lib/nrser/props/class_methods.rb', line 79

def invariant *args, &block
  .invariant *args, &block
end

#invariants(*args, &block) ⇒ Object



74
75
76
# File 'lib/nrser/props/class_methods.rb', line 74

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

#prop_for(name_or_alias, *props_args) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/nrser/props/class_methods.rb', line 65

def prop_for name_or_alias, *props_args
  sym = name_or_alias.to_sym
  
  props( *props_args ).each_value.find { |prop|
    prop.name == sym || prop.aliases.include?( sym )
  }
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