Module: ModelAttribute
- Defined in:
- lib/model_attribute.rb,
lib/model_attribute/casts.rb,
lib/model_attribute/errors.rb,
lib/model_attribute/version.rb
Defined Under Namespace
Modules: Casts, InstanceMethods
Classes: InvalidAttributeNameError, UnsupportedTypeError
Constant Summary
collapse
- SUPPORTED_TYPES =
[:integer, :boolean, :string, :time, :json]
- VERSION =
"3.1.1"
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.extended(base) ⇒ Object
9
10
11
12
13
14
|
# 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
|
Instance Method Details
#attribute(name, type, opts = {}) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# 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(" def \#{name}=(value)\n write_attribute(\#{name.inspect}, value, \#{type.inspect})\n end\n\n def \#{name}\n read_attribute(\#{name.inspect})\n end\n\n def \#{name}_changed?\n !!changes[\#{name.inspect}]\n end\n CODE\n\n if type == :boolean\n self.class_eval(<<-CODE, __FILE__, __LINE__ + 1)\n def \#{name}?\n !!read_attribute(\#{name.inspect})\n end\n CODE\n end\nend\n", __FILE__, __LINE__ + 1)
|
#attribute_defaults ⇒ Object
52
53
54
|
# File 'lib/model_attribute.rb', line 52
def attribute_defaults
@attribute_defaults
end
|
#attributes ⇒ Object
48
49
50
|
# File 'lib/model_attribute.rb', line 48
def attributes
@attribute_names
end
|