Module: AttributeExtensions::ClassMethods
- Includes:
- Typecasting
- Defined in:
- lib/attribute_extensions.rb
Instance Method Summary
collapse
#typecast, #typecaster_for
Instance Method Details
#attribute(*args) ⇒ Object
36
37
38
39
|
# File 'lib/attribute_extensions.rb', line 36
def attribute(*args)
attribute_reader(*args)
attribute_writer(*args)
end
|
#attribute_reader(*names) ⇒ Object
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/attribute_extensions.rb', line 15
def attribute_reader(*names)
options = names.
names.each do |name|
define_method(name.to_sym) do
instance_variable_set("@#{name}", options[:default]) unless instance_variable_defined?("@#{name}")
instance_variable_get("@#{name}")
end
end
end
|
#attribute_writer(*names) ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'lib/attribute_extensions.rb', line 26
def attribute_writer(*names)
options = names.
names.each do |name|
define_method("#{name}=") do |value|
instance_variable_set("@#{name}", self.class.typecast(value, options[:type]))
end
end
end
|