Class: Ransack::Nodes::Value
- Inherits:
-
Node
- Object
- Node
- Ransack::Nodes::Value
show all
- Defined in:
- lib/ransack/nodes/value.rb
Instance Attribute Summary collapse
Attributes inherited from Node
#context
Instance Method Summary
collapse
Methods inherited from Node
i18n_alias, i18n_word, #translate
Constructor Details
#initialize(context, value = nil) ⇒ Value
Returns a new instance of Value.
7
8
9
10
|
# File 'lib/ransack/nodes/value.rb', line 7
def initialize(context, value = nil)
super(context)
@value = value
end
|
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
4
5
6
|
# File 'lib/ransack/nodes/value.rb', line 4
def value
@value
end
|
Instance Method Details
#array_of_arrays?(val) ⇒ Boolean
107
108
109
|
# File 'lib/ransack/nodes/value.rb', line 107
def array_of_arrays?(val)
Array === val && Array === val.first
end
|
#cast(type) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/ransack/nodes/value.rb', line 26
def cast(type)
case type
when :date
cast_to_date(value)
when :datetime, :timestamp, :time
cast_to_time(value)
when :boolean
cast_to_boolean(value)
when :integer
cast_to_integer(value)
when :float
cast_to_float(value)
when :decimal
cast_to_decimal(value)
else
cast_to_string(value)
end
end
|
#cast_to_boolean(val) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/ransack/nodes/value.rb', line 69
def cast_to_boolean(val)
if Constants::TRUE_VALUES.include?(val)
true
elsif Constants::FALSE_VALUES.include?(val)
false
else
nil
end
end
|
#cast_to_date(val) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ransack/nodes/value.rb', line 45
def cast_to_date(val)
if val.is_a?(String)
Chronic.parse(val).in_time_zone.to_date rescue nil
elsif val.respond_to?(:to_date)
val.to_date rescue nil
else
y, m, d = *[val].flatten
m ||= 1
d ||= 1
Date.new(y,m,d) rescue nil
end
end
|
#cast_to_decimal(val) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/ransack/nodes/value.rb', line 91
def cast_to_decimal(val)
if val.blank?
nil
elsif val.class == BigDecimal
val
elsif val.respond_to?(:to_d)
val.to_d
else
val.to_s.to_d
end
end
|
#cast_to_float(val) ⇒ Object
87
88
89
|
# File 'lib/ransack/nodes/value.rb', line 87
def cast_to_float(val)
val.blank? ? nil : val.to_f
end
|
#cast_to_integer(val) ⇒ Object
83
84
85
|
# File 'lib/ransack/nodes/value.rb', line 83
def cast_to_integer(val)
val.blank? ? nil : val.to_i
end
|
#cast_to_string(val) ⇒ Object
79
80
81
|
# File 'lib/ransack/nodes/value.rb', line 79
def cast_to_string(val)
val.respond_to?(:to_s) ? val.to_s : String.new(val)
end
|
#cast_to_time(val) ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/ransack/nodes/value.rb', line 58
def cast_to_time(val)
if val.is_a?(Array)
Time.zone.local(*val) rescue nil
else
unless val.acts_like?(:time)
val = val.is_a?(String) ? Chronic.parse(val) : val.to_time rescue nil
end
val.in_time_zone rescue nil
end
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
16
17
18
19
|
# File 'lib/ransack/nodes/value.rb', line 16
def eql?(other)
self.class == other.class &&
self.value == other.value
end
|
#hash ⇒ Object
22
23
24
|
# File 'lib/ransack/nodes/value.rb', line 22
def hash
value.hash
end
|
#inspect ⇒ Object
103
104
105
|
# File 'lib/ransack/nodes/value.rb', line 103
def inspect
"Value <#{value}>"
end
|
#persisted? ⇒ Boolean
12
13
14
|
# File 'lib/ransack/nodes/value.rb', line 12
def persisted?
false
end
|