module NRSER::Props::ClassMethods
Class method “macros” that are extended in to data classes, providing the declaration interface.
Public Class Methods
from(source)
click to toggle source
Get an instance from a source.
@status
Experimental
@param [self | String
| Hash] source @return [self]
# 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
Public Instance Methods
from_data(data)
click to toggle source
Instantiate from a data hash.
@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...?
@param [#each_pair] data
@return [self]
@raise [NRSER::ArgumentError]
If `data` does not respond to `#each_pair`.
# 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)
click to toggle source
# File lib/nrser/props/class_methods.rb, line 79 def invariant *args, &block metadata.invariant *args, &block end
invariants(*args, &block)
click to toggle source
# File lib/nrser/props/class_methods.rb, line 74 def invariants *args, &block metadata.invariants *args, &block end
metadata()
click to toggle source
Get the metadata object for this class, creating it if it doesn't exist.
@return [NRSER::Props::Metadata]
# File lib/nrser/props/class_methods.rb, line 42 def metadata # 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.metadata_for self end
prop(*args, &block)
click to toggle source
# File lib/nrser/props/class_methods.rb, line 60 def prop *args, &block metadata.prop *args, &block end
prop_for(name_or_alias, *props_args)
click to toggle source
# 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)
click to toggle source
# File lib/nrser/props/class_methods.rb, line 55 def props *args, &block metadata.props *args, &block end