Module: NRSER::Meta::Props::ClassMethods

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

Overview

Methods added to the including class via ‘extend`.

Instance Method Summary collapse

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.

Instantiate from a data hash.

Parameters:

  • data (Hash<String, Object>)

Returns:

  • (self)


169
170
171
# File 'lib/nrser/meta/props.rb', line 169

def from_data data
  self.new data.symbolize_keys
end

#prop(name, **opts) ⇒ return_type

TODO:

Document prop method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/nrser/meta/props.rb', line 129

def prop name, **opts
  ref = NRSER::Meta::Props.get_props_ref self
  
  t.sym.check name
  
  if ref.key? name
    raise ArgumentError.new NRSER.squish <<-END
      Prop #{ name.inspect } already set for #{ self }:
      #{ ref[name].inspect }
    END
  end
  
  prop = Prop.new self, name, **opts
  ref[name] = prop
  
  unless prop.source?
    class_eval do
      define_method(name) do
        prop.get self
      end
      
      # protected
      #   define_method("#{ name }=") do |value|
      #     prop.set self, value
      #   end
    end
  end
end

#props(only_own: false, only_primary: false) ⇒ return_type

TODO:

Document props method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/nrser/meta/props.rb', line 98

def props only_own: false, only_primary: false
  result = if !only_own && superclass.respond_to?(:props)
    superclass.props only_own: only_own, only_primary: only_primary
  else
    {}
  end
  
  own_props = NRSER::Meta::Props.get_props_ref self
  
  if only_primary
    own_props.each {|name, prop|
      if prop.primary?
        result[name] = prop
      end
    }
  else
    result.merge! own_props
  end
  
  result
end