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_dateObject



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_timeObject



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

#!Object



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)
  # notify # why are we not notifying here
  other.object_id == object_id
end

#acts_as_string?Boolean

Returns:

  • (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_booleanObject



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_dateObject



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_datetimeObject 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_floatObject 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_integerObject 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_nilObject



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_stringObject 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)
  # notify # why are we not notifying here
  return @object.coerce(s) if @object
  [__send__("to_#{s.class.name.downcase}"), s]
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


93
94
95
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 93

def loaded?
  false
end

#loading?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 89

def loading?
  true
end

#nil?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 101

def nil?
  true
end

#notifyObject



83
84
85
86
87
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 83

def notify
  return if ReactiveRecord::Base.data_loading?
  ReactiveRecord.loads_pending!
  ReactiveRecord::WhileLoading.loading!
end

#object_idObject



137
138
139
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 137

def object_id
  `self.$$id`
end

#present?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 97

def present?
  false
end

#to_dateObject



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

#to_fObject



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

#to_iObject



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_numberObject



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_numericObject



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_sObject 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

#to_timeObject



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

Returns:

  • (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