class Scopie::Value

Constants

TRUE_VALUES

Public Class Methods

new(hash, key_name, options) click to toggle source
# File lib/scopie/value.rb, line 7
def initialize(hash, key_name, options)
  @hash = hash
  @key_name = key_name
  @options = options
end

Public Instance Methods

allow_blank?() click to toggle source
# File lib/scopie/value.rb, line 30
def allow_blank?
  @options[:allow_blank]
end
coerced() click to toggle source
# File lib/scopie/value.rb, line 17
def coerced
  coerce_to_type(raw, fetch_type)
end
given?() click to toggle source
# File lib/scopie/value.rb, line 21
def given?
  key_passed? || has_default?
end
present?() click to toggle source
# File lib/scopie/value.rb, line 25
def present?
  value = raw
  value.respond_to?(:empty?) ? !value.empty? : !!value
end
raw() click to toggle source
# File lib/scopie/value.rb, line 13
def raw
  @hash.fetch(@key_name) { fetch_default }
end

Private Instance Methods

coerce_to_boolean(value) click to toggle source
# File lib/scopie/value.rb, line 62
def coerce_to_boolean(value)
  TRUE_VALUES.include? value
end
coerce_to_date(value) click to toggle source
# File lib/scopie/value.rb, line 70
def coerce_to_date(value)
  Date.parse(value)
end
coerce_to_float(value) click to toggle source
# File lib/scopie/value.rb, line 74
def coerce_to_float(value)
  Float(value)
end
coerce_to_integer(value) click to toggle source
# File lib/scopie/value.rb, line 66
def coerce_to_integer(value)
  Integer(value)
end
coerce_to_type(value, type) click to toggle source
# File lib/scopie/value.rb, line 52
def coerce_to_type(value, type)
  return value unless type

  coercion_method_name = "coerce_to_#{type}"

  respond_to?(coercion_method_name, true) || raise(Scopie::InvalidTypeError.new(type))

  send(coercion_method_name, value)
end
fetch_default() click to toggle source
# File lib/scopie/value.rb, line 40
def fetch_default
  @options[:default]
end
fetch_type() click to toggle source
# File lib/scopie/value.rb, line 36
def fetch_type
  @options[:type]
end
has_default?() click to toggle source
# File lib/scopie/value.rb, line 44
def has_default?
  @options.key?(:default)
end
key_passed?() click to toggle source
# File lib/scopie/value.rb, line 48
def key_passed?
  @hash.key?(@key_name)
end