Module: AttributeExtensions::ClassMethods

Includes:
Typecasting
Defined in:
lib/attribute_extensions.rb

Instance Method Summary collapse

Methods included from Typecasting

#typecast, #typecaster_for

Instance Method Details

#attribute(name, options = {}) ⇒ Object



28
29
30
31
# File 'lib/attribute_extensions.rb', line 28

def attribute(name, options={})
  attribute_reader(name, options)
  attribute_writer(name, options)
end

#attribute_reader(name, options = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/attribute_extensions.rb', line 15

def attribute_reader(name, options={})
  define_method(name.to_sym) do
    instance_variable_set("@#{name}", options[:default]) unless instance_variable_defined?("@#{name}")
    instance_variable_get("@#{name}")
  end
end

#attribute_writer(name, options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/attribute_extensions.rb', line 22

def attribute_writer(name, options={})
  define_method("#{name}=") do |value|
    instance_variable_set("@#{name}", self.class.typecast(value, options[:type]))
  end
end