Module: Pod4::TypeCasting::InstanceMethods

Defined in:
lib/pod4/typecasting.rb

Instance Method Summary collapse

Instance Method Details

#guard(ot) ⇒ Object

set Octothorpe Guards for everything in the given OT, based on the typecast settings.



243
244
245
246
247
248
# File 'lib/pod4/typecasting.rb', line 243

def guard(ot)
  self.class.typecasts.each do |fld, tc|
    type = tc[:ot_as] || tc[:as]
    set_guard(ot, fld, type) if type
  end
end

#map_to_interfaceObject



182
183
184
185
186
# File 'lib/pod4/typecasting.rb', line 182

def map_to_interface
  ot   = super
  hash = typecast_ot(ot, strict: true)
  ot.merge hash
end

#map_to_model(ot) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/pod4/typecasting.rb', line 167

def map_to_model(ot)
  enc = self.class.encoding
  
  ot.each_value do |v|
    v.force_encoding(enc) if v.kind_of?(String) && enc
  end

  super(ot)
end

#set(ot) ⇒ Object



177
178
179
180
# File 'lib/pod4/typecasting.rb', line 177

def set(ot)
  hash = typecast_ot(ot)
  super(ot.merge hash)
end

#to_otObject



188
189
190
191
192
193
194
195
196
197
# File 'lib/pod4/typecasting.rb', line 188

def to_ot
  ot  = super
  ot2 = ot.merge typecast_ot_to_ot(ot)

  self.class.typecasts.each do |fld, tc|
    set_guard(ot2, fld, tc[:ot_as]) if tc[:ot_as]
  end

  ot2
end

#typecast(type, thing, opt = {}) ⇒ Object

Return thing cast to type. If opt is true, then return nil if thing cannot be cast to type; otherwise return thing unchanged.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/pod4/typecasting.rb', line 203

def typecast(type, thing, opt={})
  # Nothing to do
  return thing if type.is_a?(Class) && thing.is_a?(type)

  # Nothing wrong with nil for our purposes; it's always allowed
  return thing if thing.nil?

  # For all current cases, attempting to typecast a blank string should return nil
  return nil if thing =~ /\A\s*\Z/ 

  # The order we try these in matters
  return tc_bigdecimal(thing) if type == BigDecimal 
  return tc_float(thing)      if type == Float      
  return tc_integer(thing)    if type == Integer    
  return tc_date(thing)       if type == Date       
  return tc_time(thing)       if type == Time       
  return tc_boolean(thing)    if type == :boolean   

  fail Pod4Error, "Bad type passed to typecast()"
rescue ArgumentError
  return (opt[:strict] ? nil : thing)
end

#typecast?(attr, val = nil) ⇒ Boolean

Return true if the attribute can be cast to the given value. You must name an attribute you specified in a typecast declaration, or you will get an exception. You may pass a value to test, or failing that, we take the current value of the attribute.

Returns:

  • (Boolean)


232
233
234
235
236
237
238
# File 'lib/pod4/typecasting.rb', line 232

def typecast?(attr, val=nil)
  fail Pod4Error, "Unknown column passed to typecast?()" \
    unless (tc = self.class.typecasts[attr])

  val = instance_variable_get("@#{attr}".to_sym) if val.nil?
  !typecast_one(val, tc.merge(strict: true)).nil?
end