lib/volt/models/field_helpers.rb
# field lets you declare your fields instead of using the underscore syntax.
# An optional class restriction can be passed in.
field lets you declare your fields instead of using the underscore syntax.
An optional class restriction can be passed in.
field lets you declare your fields instead of using the underscore syntax . An optional class restriction can be passed in .
def field(name, klass=nil)
      if klass && ![String, Numeric].include?(klass)
        raise FieldHelpers::InvalidFieldClass, "valid field types is currently limited to String or Numeric"
      end
      define_method(name) do
        read_attribute(name)
      end
      define_method(:"#{name}=") do |val|
        if klass
          if klass == String
            val = val.to_s
          elsif klass == Numeric
            val = val.to_f
          end
        end
        assign_attribute(name, val)
      end
    end
def field ( name , klass = nil ) if klass && ! [ String , Numeric ] . include? ( klass ) raise FieldHelpers :: InvalidFieldClass , "valid field types is currently limited to String or Numeric" end define_method ( name ) do read_attribute ( name ) end define_method ( :" #{ name } = " ) do | val | if klass if klass == String val = val . to_s elsif klass == Numeric val = val . to_f end end assign_attribute ( name , val ) end end
field lets you declare your fields instead of using the underscore syntax.
field lets you declare your fields instead of using the underscore syntax .
# Check if the value assigned matches the class restriction # Cast to the right type
name
klass