Class: ReactiveRecord::Base::DummyValue
- Inherits:
-
BasicObject
- Defined in:
- lib/reactive_record/active_record/reactive_record/dummy_value.rb
Overview
DummyValue uses the ActiveRecord type info to act like an appropriate loaded value.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(column_hash = nil) ⇒ DummyValue
Returns a new instance of DummyValue.
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 25
def initialize(column_hash = nil)
column_hash ||= {}
notify
@column_hash = column_hash
column_type = (
@column_hash[:sql_type_metadata] &&
@column_hash[:sql_type_metadata][:type]
) || 'nil'
default_value_method = "build_default_value_for_#{column_type}"
@object = __send__ default_value_method
rescue Exception
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 109
def method_missing(method, *args, &block)
if method =~ /^build_default_value_for_/
nil
elsif @object || @object.respond_to?(method)
notify
@object.send method, *args, &block
elsif 0.respond_to? method
notify
0.send(method, *args, &block)
elsif ''.respond_to? method
notify
''.send(method, *args, &block)
else
super
end
end
|
Class Method Details
.dummy_date ⇒ Object
188
189
190
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 188
def self.dummy_date
@dummy_date ||= Date.parse('1/1/2001')
end
|
.dummy_time ⇒ Object
184
185
186
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 184
def self.dummy_time
@dummy_time ||= Time.parse('2001-01-01T00:00:00.000-00:00')
end
|
Instance Method Details
105
106
107
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 105
def !
true
end
|
#==(other) ⇒ Object
132
133
134
135
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 132
def ==(other)
other.object_id == object_id
end
|
#acts_as_string? ⇒ Boolean
204
205
206
207
208
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 204
def acts_as_string?
return true if @object.is_a? String
return @object.acts_as_string? if @object
true
end
|
#build_default_value_for_boolean ⇒ Object
61
62
63
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 61
def build_default_value_for_boolean
@column_hash[:default] || false
end
|
#build_default_value_for_date ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 53
def build_default_value_for_date
if @column_hash[:default]
Date.parse(@column_hash[:default])
else
::ReactiveRecord::Base::DummyValue.dummy_date
end
end
|
#build_default_value_for_datetime ⇒ Object
Also known as:
build_default_value_for_time, build_default_value_for_timestamp
42
43
44
45
46
47
48
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 42
def build_default_value_for_datetime
if @column_hash[:default]
Time.parse(@column_hash[:default].gsub(' ','T')+'+00:00')
else
::ReactiveRecord::Base::DummyValue.dummy_time
end
end
|
#build_default_value_for_float ⇒ Object
Also known as:
build_default_value_for_decimal
65
66
67
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 65
def build_default_value_for_float
@column_hash[:default] || Float(0.0)
end
|
#build_default_value_for_integer ⇒ Object
Also known as:
build_default_value_for_bigint
71
72
73
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 71
def build_default_value_for_integer
@column_hash[:default] || Integer(0)
end
|
#build_default_value_for_nil ⇒ Object
38
39
40
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 38
def build_default_value_for_nil
@column_hash[:default] || nil
end
|
#build_default_value_for_string ⇒ Object
Also known as:
build_default_value_for_text
77
78
79
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 77
def build_default_value_for_string
@column_hash[:default] || ''
end
|
#coerce(s) ⇒ Object
126
127
128
129
130
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 126
def coerce(s)
return @object.coerce(s) if @object
[__send__("to_#{s.class.name.downcase}"), s]
end
|
#is_a?(klass) ⇒ Boolean
141
142
143
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 141
def is_a?(klass)
klass == ::ReactiveRecord::Base::DummyValue
end
|
#loaded? ⇒ Boolean
93
94
95
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 93
def loaded?
false
end
|
#loading? ⇒ Boolean
89
90
91
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 89
def loading?
true
end
|
#nil? ⇒ Boolean
101
102
103
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 101
def nil?
true
end
|
#object_id ⇒ Object
137
138
139
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 137
def object_id
`self.$$id`
end
|
#present? ⇒ Boolean
97
98
99
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 97
def present?
false
end
|
192
193
194
195
196
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 192
def to_date
notify
return @object.to_date if @object
ReactiveRecord::Base::DummyValue.dummy_date
end
|
160
161
162
163
164
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 160
def to_f
notify
return @object.to_f if @object
0.0
end
|
166
167
168
169
170
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 166
def to_i
notify
return @object.to_i if @object
0
end
|
#to_number ⇒ Object
178
179
180
181
182
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 178
def to_number
notify
return @object.to_number if @object
0
end
|
#to_numeric ⇒ Object
172
173
174
175
176
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 172
def to_numeric
notify
return @object.to_numeric if @object
0
end
|
#to_s ⇒ Object
Also known as:
inspect
150
151
152
153
154
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 150
def to_s
notify
return @object.to_s if @object
''
end
|
198
199
200
201
202
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 198
def to_time
notify
return @object.to_time if @object
ReactiveRecord::Base::DummyValue.dummy_time
end
|
#try(*args, &b) ⇒ Object
210
211
212
213
214
215
216
217
218
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 210
def try(*args, &b)
if args.empty? && block_given?
yield self
else
__send__(*args, &b)
end
rescue
nil
end
|
#zero? ⇒ Boolean
145
146
147
148
|
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 145
def zero?
return @object.zero? if @object
false
end
|