module ModelAttribute
Constants
- SUPPORTED_TYPES
- VERSION
Public Class Methods
extended(base)
click to toggle source
# File lib/model_attribute.rb, line 9 def self.extended(base) base.send(:include, InstanceMethods) base.instance_variable_set('@attribute_names', []) base.instance_variable_set('@attribute_types', {}) base.instance_variable_set('@attribute_defaults', {}) end
Public Instance Methods
attribute(name, type, opts = {})
click to toggle source
# File lib/model_attribute.rb, line 16 def attribute(name, type, opts = {}) name = name.to_sym type = type.to_sym raise UnsupportedTypeError.new(type) unless SUPPORTED_TYPES.include?(type) @attribute_names << name @attribute_types[name] = type @attribute_defaults[name] = opts[:default] if opts.key?(:default) self.class_eval(<<-CODE, __FILE__, __LINE__ + 1) def #{name}=(value) write_attribute(#{name.inspect}, value, #{type.inspect}) end def #{name} read_attribute(#{name.inspect}) end def #{name}_changed? !!changes[#{name.inspect}] end CODE if type == :boolean self.class_eval(<<-CODE, __FILE__, __LINE__ + 1) def #{name}? !!read_attribute(#{name.inspect}) end CODE end end
attribute_defaults()
click to toggle source
# File lib/model_attribute.rb, line 52 def attribute_defaults @attribute_defaults end
attributes()
click to toggle source
# File lib/model_attribute.rb, line 48 def attributes @attribute_names end