Class: DateValues::Rails::YearMonthType

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/date_values/rails/year_month_type.rb

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/date_values/rails/year_month_type.rb', line 10

def cast(value)
  case value
  when YearMonth then value
  when Hash      then cast_hash(value)
  when String    then YearMonth.parse(value)
  when nil       then nil
  end
rescue ArgumentError
  nil
end

#deserialize(value) ⇒ Object



25
26
27
28
29
# File 'lib/date_values/rails/year_month_type.rb', line 25

def deserialize(value)
  return nil if value.nil?

  YearMonth.parse(value)
end

#serialize(value) ⇒ Object



21
22
23
# File 'lib/date_values/rails/year_month_type.rb', line 21

def serialize(value)
  value&.to_s
end

#typeObject



6
7
8
# File 'lib/date_values/rails/year_month_type.rb', line 6

def type
  :year_month
end