Module: FastAttributes
- Defined in:
- lib/fast_attributes.rb,
lib/fast_attributes/builder.rb,
lib/fast_attributes/version.rb,
lib/fast_attributes/type_cast.rb,
lib/fast_attributes/default_attributes.rb
Defined Under Namespace
Classes: Builder, TypeCast, UnsupportedTypeError
Constant Summary
collapse
- TRUE_VALUES =
{true => nil, 1 => nil, '1' => nil, 't' => nil, 'T' => nil, 'true' => nil, 'TRUE' => nil, 'on' => nil, 'ON' => nil}
- FALSE_VALUES =
{false => nil, 0 => nil, '0' => nil, 'f' => nil, 'F' => nil, 'false' => nil, 'FALSE' => nil, 'off' => nil, 'OFF' => nil}
- VERSION =
'0.9.0'
- SINGLETON_CLASSES =
[::NilClass, ::TrueClass, ::FalseClass, ::Numeric, ::Symbol].freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.add_default_attribute(klass, attribute, value) ⇒ Object
18
19
20
21
22
|
# File 'lib/fast_attributes/default_attributes.rb', line 18
def add_default_attribute(klass, attribute, value)
@default_attributes ||= {}
@default_attributes[klass] ||= {}
@default_attributes[klass][attribute] = value
end
|
.cloneable?(value) ⇒ Boolean
24
25
26
27
28
29
30
31
|
# File 'lib/fast_attributes/default_attributes.rb', line 24
def cloneable?(value)
case value
when *SINGLETON_CLASSES
false
else
true
end
end
|
.default_attributes(klass) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/fast_attributes/default_attributes.rb', line 5
def default_attributes(klass)
return {} unless (@default_attributes || {})[klass]
@default_attributes[klass].each_with_object({}) do |(attribute, value), memo|
memo[attribute] = if value.respond_to?(:call)
value.call
elsif cloneable?(value)
value.clone
else
value
end
end
end
|
.get_type_casting(klass) ⇒ Object
18
19
20
|
# File 'lib/fast_attributes.rb', line 18
def get_type_casting(klass)
type_casting[klass]
end
|
.remove_type_casting(klass) ⇒ Object
31
32
33
|
# File 'lib/fast_attributes.rb', line 31
def remove_type_casting(klass)
type_casting.delete(klass)
end
|
.set_type_casting(klass, casting) ⇒ Object
22
23
24
25
26
27
28
29
|
# File 'lib/fast_attributes.rb', line 22
def set_type_casting(klass, casting)
symbol = klass.name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.to_sym type_cast symbol, klass do
from 'nil', to: 'nil'
from klass.name, to: '%s'
otherwise casting
end
end
|
.type_cast(*types_or_classes, &block) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/fast_attributes.rb', line 39
def type_cast(*types_or_classes, &block)
types_or_classes.each do |type_or_class|
type_cast = TypeCast.new(type_or_class)
type_cast.instance_eval(&block)
type_casting[type_or_class] = type_cast
end
end
|
.type_casting ⇒ Object
14
15
16
|
# File 'lib/fast_attributes.rb', line 14
def type_casting
@type_casting ||= {}
end
|
.type_exists?(klass) ⇒ Boolean
35
36
37
|
# File 'lib/fast_attributes.rb', line 35
def type_exists?(klass)
type_casting.has_key?(klass)
end
|
Instance Method Details
#attribute(*attributes, type) ⇒ Object
54
55
56
57
58
|
# File 'lib/fast_attributes.rb', line 54
def attribute(*attributes, type)
builder = Builder.new(self)
builder.attribute *attributes, type
builder.compile!
end
|
#define_attributes(options = {}, &block) ⇒ Object
48
49
50
51
52
|
# File 'lib/fast_attributes.rb', line 48
def define_attributes(options = {}, &block)
builder = Builder.new(self, options)
builder.instance_eval(&block)
builder.compile!
end
|