class AttributesDSL::Attribute

Describes settings for PORO attribute

@api private

@author Andrew Kozin <Andrew.Kozin@gmail.com>

Attributes

name[R]

@!attribute [r] name

@return [Symbol] the name of the attribute

reader[R]

@!attribute [r] reader

@return [Boolean] whether an attribute should be readable

Public Class Methods

new(name, options = {}, &coercer) click to toggle source

Initializes the attribute

@param [#to_sym] name @param [Hash] options @param [Proc] coercer

# File lib/attributes_dsl/attribute.rb, line 32
def initialize(name, options = {}, &coercer)
  @name    = name.to_sym
  @options = { coercer: coercer }.merge(options)
  @reader  = @options.fetch(:reader) { true }
end

Public Instance Methods

transformer() click to toggle source

A proc that transform a hash of attributes using current settings

@return [Proc]

# File lib/attributes_dsl/attribute.rb, line 42
def transformer
  convert unless @options.empty?
end

Private Instance Methods

absence() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 56
def absence
  [missed, default].compact.reduce(:>>) || identity
end
blacklist() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 76
def blacklist
  Transprocs[:blacklist, name, @options[:except]] if @options[:except]
end
coercer() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 80
def coercer
  Transprocs[:coerce, name, @options[:coercer]] if @options[:coercer]
end
convert() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 48
def convert
  @convert ||= Transprocs[:convert, name, presence, absence]
end
default() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 68
def default
  Transprocs[:default, name, @options[:default]] if @options[:default]
end
identity() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 60
def identity
  Transprocs[:identity]
end
missed() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 64
def missed
  Transprocs[:missed, name] if @options[:required]
end
presence() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 52
def presence
  [whitelist, blacklist, coercer].compact.reduce(:>>) || identity
end
whitelist() click to toggle source
# File lib/attributes_dsl/attribute.rb, line 72
def whitelist
  Transprocs[:whitelist, name, @options[:only]] if @options[:only]
end