class AttributesDSL::Attributes
Describes a collection of attributes declaration with methods to validate and extract instance attributes from a hash.
@api private
@author Andrew Kozin <Andrew.Kozin@gmail.com>
Public Instance Methods
add(name, options = {}, &coercer)
click to toggle source
Initializes the attribute from given arguments and returns new immutable collection with the attribute
@param (see Attribute#initialize)
@return [AttributesDSL::Attributes]
# File lib/attributes_dsl/attributes.rb, line 30 def add(name, options = {}, &coercer) name = name.to_sym value = Attribute.new(name, options, &coercer) clone_with do @attributes = attributes.merge(name => value) @transformer = nil end end
attributes()
click to toggle source
@!attribute [r] attributes
Uses the set of attributes to ensure their uniqueness (by name)
@return [Set] the set of registered attributes
# File lib/attributes_dsl/attributes.rb, line 19 def attributes @attributes ||= {} end
reader?(name)
click to toggle source
Checks whether an attribute reader should be defined
@param [#to_sym] name
@return [Boolean]
# File lib/attributes_dsl/attributes.rb, line 53 def reader?(name) attributes[name.to_sym].reader end
transformer()
click to toggle source
Returns the proc that converts a hash of attributes using current setting
@return [Proc]
# File lib/attributes_dsl/attributes.rb, line 43 def transformer @transformer ||= transprocs.flatten.compact.reduce(:>>) end
Private Instance Methods
clone_with(&block)
click to toggle source
# File lib/attributes_dsl/attributes.rb, line 71 def clone_with(&block) dup.tap { |instance| instance.instance_eval(&block) } end
keys()
click to toggle source
# File lib/attributes_dsl/attributes.rb, line 67 def keys attributes.keys end
transprocs()
click to toggle source
# File lib/attributes_dsl/attributes.rb, line 59 def transprocs [ Transprocs[:filter, keys], attributes.values.map(&:transformer), Transprocs[:update, keys] ] end