Sha256: 07a8de40e17e4841e8cf41d4d436c37a5afd9d6ceb3b11f6e437d8f5a0e8000f
Contents?: true
Size: 985 Bytes
Versions: 1
Compression:
Stored size: 985 Bytes
Contents
# Provides a method to setup a field on a model. module FieldHelpers class InvalidFieldClass < RuntimeError; end module ClassMethods # 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) fail 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| # Check if the value assigned matches the class restriction if klass # Cast to the right type if klass == String val = val.to_s elsif klass == Numeric val = val.to_f end end assign_attribute(name, val) end end end def self.included(base) base.send :extend, ClassMethods end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
volt-0.8.27.beta3 | lib/volt/models/field_helpers.rb |