Module: Katalyst::Tables::Collection::Type::Helpers::Range

Extended by:
ActiveSupport::Concern
Included in:
Date, Float, Integer
Defined in:
lib/katalyst/tables/collection/type/helpers/range.rb

Overview

Adds support for ranges

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/katalyst/tables/collection/type/helpers/range.rb', line 64

def cast(value)
  case value
  when nil
    nil
  when ::Range
    value
  when self.class.const_get(:SINGLE_VALUE)
    super($~[:value])
  when self.class.const_get(:LOWER_BOUND)
    make_range(super($~[:lower]), nil)
  when self.class.const_get(:UPPER_BOUND)
    make_range(nil, super($~[:upper]))
  when self.class.const_get(:BOUNDED)
    make_range(super($~[:lower]), super($~[:upper]))
  else
    super
  end
end

#deserialize(value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/katalyst/tables/collection/type/helpers/range.rb', line 22

def deserialize(value)
  if value.is_a?(::Range)
    if value.begin.nil?
      make_range(nil, deserialize(value.end))
    elsif value.end.nil?
      make_range(deserialize(value.begin), nil)
    else
      make_range(deserialize(value.begin), deserialize(value.end))
    end
  else
    super
  end
end

#serialize(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/katalyst/tables/collection/type/helpers/range.rb', line 36

def serialize(value)
  if value.is_a?(::Range)
    if value.begin.nil?
      make_range(nil, serialize(value.end))
    elsif value.end.nil?
      make_range(serialize(value.begin), nil)
    else
      make_range(serialize(value.begin), serialize(value.end))
    end
  else
    super
  end
end

#to_param(value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/katalyst/tables/collection/type/helpers/range.rb', line 50

def to_param(value)
  if value.is_a?(::Range)
    if value.begin.nil?
      "..#{to_param(value.end)}"
    elsif value.end.nil?
      "#{to_param(value.begin)}.."
    else
      "#{to_param(value.begin)}..#{to_param(value.end)}"
    end
  else
    super
  end
end