Module: Pod4::TypeCasting::InstanceMethods
- Defined in:
- lib/pod4/typecasting.rb
Instance Method Summary collapse
-
#guard(ot) ⇒ Object
set Octothorpe Guards for everything in the given OT, based on the typecast settings.
- #map_to_interface ⇒ Object
- #map_to_model(ot) ⇒ Object
- #set(ot) ⇒ Object
- #to_ot ⇒ Object
-
#typecast(type, thing, opt = {}) ⇒ Object
Return thing cast to type.
-
#typecast?(attr, val = nil) ⇒ Boolean
Return true if the attribute can be cast to the given value.
Instance Method Details
#guard(ot) ⇒ Object
set Octothorpe Guards for everything in the given OT, based on the typecast settings.
257 258 259 260 261 262 |
# File 'lib/pod4/typecasting.rb', line 257 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_interface ⇒ Object
196 197 198 199 200 |
# File 'lib/pod4/typecasting.rb', line 196 def map_to_interface ot = super hash = typecast_ot(ot, strict: true, mode: :map_to_interface) ot.merge hash end |
#map_to_model(ot) ⇒ Object
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/pod4/typecasting.rb', line 180 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 hash = typecast_ot(ot, mode: :map_to_model) super(ot.merge hash) end |
#set(ot) ⇒ Object
191 192 193 194 |
# File 'lib/pod4/typecasting.rb', line 191 def set(ot) hash = typecast_ot(ot, mode: :set) super(ot.merge hash) end |
#to_ot ⇒ Object
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/pod4/typecasting.rb', line 202 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.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/pod4/typecasting.rb', line 217 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.
246 247 248 249 250 251 252 |
# File 'lib/pod4/typecasting.rb', line 246 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, mode: :typecast?)).nil? end |