Class: ActiveDateRange::DateRangeType

Inherits:
ActiveModel::Type::String
  • Object
show all
Defined in:
lib/active_date_range/active_model_type.rb

Instance Method Summary collapse

Constructor Details

#initialize(safe_parse: false) ⇒ DateRangeType

Pass safe_parse: true for attributes fed by input you don't control, like a query parameter. The attribute then casts through .safe_parse, so it returns nil instead of raising:

attribute :period, :date_range, safe_parse: true


10
11
12
13
# File 'lib/active_date_range/active_model_type.rb', line 10

def initialize(safe_parse: false)
  @safe_parse = safe_parse
  super()
end

Instance Method Details

#cast(value) ⇒ Object



15
16
17
18
19
# File 'lib/active_date_range/active_model_type.rb', line 15

def cast(value)
  return ActiveDateRange::DateRange.safe_parse(value) if @safe_parse

  ActiveDateRange::DateRange.parse(value)
end