class Alphred::Struct::Attribute

Attributes

coerce[R]
enum[R]
name[R]

Public Class Methods

new(name, **options) click to toggle source
# File lib/alphred/struct.rb, line 52
def initialize(name, **options)
  @name = name
  @required = options.fetch(:required, false)
  @coerce = options.fetch(:coerce, ->(x) { x })
  @enum = options[:enum]
end

Public Instance Methods

required?() click to toggle source
# File lib/alphred/struct.rb, line 59
def required?
  !!@required
end
value_from(hash) click to toggle source
# File lib/alphred/struct.rb, line 63
def value_from(hash)
  uncoerced = hash.fetch(name) { raise RequiredAttributeError.new(name) }
  coerced = coerce.call(uncoerced)
  raise InvalidEnumError.new(coerced) if enum && !enum.include?(coerced)
  { name => coerced }
end