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
# 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 = Base.column_type(@column_hash) || '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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 106

def method_missing(method, *args, &block)
  if method.start_with?("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



190
191
192
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 190

def self.dummy_date
  @dummy_date ||= ::Date.parse('1/1/2001')
end

.dummy_timeObject



186
187
188
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 186

def self.dummy_time
  @dummy_time ||= ::Time.parse('2001-01-01T00:00:00.000-00:00')
end

Instance Method Details

#!Object



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

def !
  true
end

#==(other) ⇒ Object



129
130
131
132
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 129

def ==(other)
  # notify # why are we not notifying here
  other.object_id == object_id
end

#acts_as_string?Boolean

Returns:

  • (Boolean)


206
207
208
209
210
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 206

def acts_as_string?
  return true if @object.is_a? ::String
  return @object.acts_as_string? if @object && @object.respond_to?(:acts_as_string?)
  true
end

#build_default_value_for_booleanObject



58
59
60
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 58

def build_default_value_for_boolean
  @column_hash[:default] || false
end

#build_default_value_for_dateObject



50
51
52
53
54
55
56
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 50

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



39
40
41
42
43
44
45
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 39

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



62
63
64
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 62

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



68
69
70
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 68

def build_default_value_for_integer
  @column_hash[:default] || Integer(0)
end

#build_default_value_for_nilObject



35
36
37
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 35

def build_default_value_for_nil
  @column_hash[:default] || nil
end

#build_default_value_for_stringObject Also known as: build_default_value_for_text



74
75
76
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 74

def build_default_value_for_string
  @column_hash[:default] || ''
end

#coerce(s) ⇒ Object



123
124
125
126
127
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 123

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)


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

def is_a?(klass)
  klass == ::ReactiveRecord::Base::DummyValue
end

#loaded?Boolean

Returns:

  • (Boolean)


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

def loaded?
  false
end

#loading?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 86

def loading?
  true
end

#nil?Boolean

Returns:

  • (Boolean)


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

def nil?
  true
end

#notifyObject



80
81
82
83
84
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 80

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

#object_idObject



134
135
136
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 134

def object_id
  `self.$$id`
end

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  false
end

#respond_to?(method) ⇒ Boolean

this is a hackish way and compatible with any other rendered object to identify a DummyValue during render in ReactRenderingContext.run_child_block() and to convert it to a string, for rendering advantage over a try(:method) is, that it doesnt raise und thus is faster which is important during render

Returns:

  • (Boolean)


218
219
220
221
222
223
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 218

def respond_to?(method)
  return true if method == :acts_as_string?
  return true if %i[inspect to_date to_f to_i to_numeric to_number to_s to_time].include? method
  return @object.respond_to? if @object
  false
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



153
154
155
156
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 153

def tap
  yield self
  self
end

#to_dateObject



194
195
196
197
198
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 194

def to_date
  notify
  return @object.to_date if @object
  ::ReactiveRecord::Base::DummyValue.dummy_date
end

#to_fObject



162
163
164
165
166
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 162

def to_f
  notify
  return @object.to_f if @object
  0.0
end

#to_iObject



168
169
170
171
172
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 168

def to_i
  notify
  return @object.to_i if @object
  0
end

#to_numberObject



180
181
182
183
184
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 180

def to_number
  notify
  return @object.to_number if @object
  0
end

#to_numericObject



174
175
176
177
178
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 174

def to_numeric
  notify
  return @object.to_numeric if @object
  0
end

#to_sObject Also known as: inspect



147
148
149
150
151
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 147

def to_s
  notify
  return @object.to_s if @object
  ''
end

#to_timeObject



200
201
202
203
204
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 200

def to_time
  notify
  return @object.to_time if @object
  ::ReactiveRecord::Base::DummyValue.dummy_time
end

#try(*args, &b) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 225

def try(*args, &b)
  if args.empty? && block_given?
    yield self
  else
    __send__(*args, &b)
  end
rescue ::Exception
  nil
end

#zero?Boolean

Returns:

  • (Boolean)


142
143
144
145
# File 'lib/reactive_record/active_record/reactive_record/dummy_value.rb', line 142

def zero?
  return @object.zero? if @object
  false
end