class SimpleParams::Attribute

Attributes

default[R]
formatter[R]
name[R]
parent[R]
type[R]
validations[R]

Public Class Methods

new(parent, name, opts={}) click to toggle source
# File lib/simple_params/attribute.rb, line 9
def initialize(parent, name, opts={})
  @parent = parent
  @name = name.to_sym
  @type = TYPE_MAPPINGS[opts[:type]]
  @value = nil
  @default = opts[:default]
  @formatter = opts[:formatter]
  @validations = opts[:validations] || {}
end

Public Instance Methods

raw_value() click to toggle source
# File lib/simple_params/attribute.rb, line 19
def raw_value
  empty = @value.nil? || (@value.is_a?(String) && @value.blank?)
  empty ? raw_default : @value
end
value() click to toggle source
# File lib/simple_params/attribute.rb, line 24
def value
  return raw_value if raw_value.blank?
  if @formatter.present?
    Formatter.new(@parent, @formatter).format(raw_value)
  else
    raw_value
  end
end
value=(val) click to toggle source
# File lib/simple_params/attribute.rb, line 33
def value=(val)
  @value = if @type.present?
    virtus_attr = Virtus::Attribute.build(@type)
    virtus_attr.coerce(val)
  else
    val
  end
end

Private Instance Methods

raw_default() click to toggle source
# File lib/simple_params/attribute.rb, line 43
def raw_default
  if @default.is_a?(Proc)
    @default.call(parent, self)
  else
    @default
  end
end