Module: AttrTyped

Defined in:
lib/attr_typed.rb,
lib/attr_typed/version.rb

Overview

Ability to mix in some strong typing support for attributes

Usage:

attr_typed my_field: money,
           another_field: time

Then on assigning my_field = “1.23”, my_field will be co-erced to Money.new(123) ($1.23)

Gotchas:

- Don't use instance variable assignment (Use self.my_field= as opposed to @my_field=)
- Due to the way string.to_f works in ruby, self.my_field = "cats" will equal $0.00

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"1.0.5"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



30
31
32
# File 'lib/attr_typed.rb', line 30

def logger
  @logger
end

Class Method Details

.included(klass) ⇒ Object



25
26
27
# File 'lib/attr_typed.rb', line 25

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#parse_big_decimal(value) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/attr_typed.rb', line 75

def parse_big_decimal(value)
  return value if value.is_a?(BigDecimal)
  return value.to_d if value.is_a?(Float)

  BigDecimal(value)
rescue ArgumentError
  BigDecimal(0)
end

#parse_boolean(value) ⇒ Object



100
101
102
103
104
105
# File 'lib/attr_typed.rb', line 100

def parse_boolean(value)
  return if value.nil? && !value.is_a?(FalseClass)
  return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)

  %w[true y].include?(value.to_s.downcase)
end

#parse_date(value) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/attr_typed.rb', line 59

def parse_date(value)
  return value if value.is_a?(Date)

  if Time.respond_to?(:zone)
    Time.zone.parse(value).to_date
  else
    Date.parse(value)
  end
end

#parse_date_time(value) ⇒ Object



69
70
71
72
73
# File 'lib/attr_typed.rb', line 69

def parse_date_time(value)
  return value if value.is_a?(DateTime)

  DateTime.parse(value)
end

#parse_integer(value) ⇒ Object



46
47
48
# File 'lib/attr_typed.rb', line 46

def parse_integer(value)
  value.to_i
end

#parse_money(value) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/attr_typed.rb', line 84

def parse_money(value)
  return value if value.is_a?(Money)

  Monetize.from_bigdecimal(BigDecimal(value.to_s))
rescue ArgumentError
  Money.new(0)
end

#parse_strict_integer(value) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/attr_typed.rb', line 50

def parse_strict_integer(value)
  return value if value.nil? || value.is_a?(Integer)
  return value.to_i if value.is_a?(Float) || value.is_a?(BigDecimal)

  Integer(value, 10)
rescue ArgumentError
  nil
end

#parse_string(value) ⇒ Object



40
41
42
43
44
# File 'lib/attr_typed.rb', line 40

def parse_string(value)
  return value if value.is_a?(String)

  value.to_s
end

#parse_time(value) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/attr_typed.rb', line 92

def parse_time(value)
  return value if value.is_a?(Time)

  raise "ActiveSupport with a time zone set is required" unless Time.respond_to?(:zone)

  Time.zone.parse(value)
end

#parse_typed_value(value, type) ⇒ Object



33
34
35
36
37
38
# File 'lib/attr_typed.rb', line 33

def parse_typed_value(value, type)
  public_send("parse_#{type}", value) unless value.nil?
rescue ArgumentError => e
  AttrTyped&.logger&.error("Error parsing '#{value}' into a #{type}")
  raise e
end