Module: AttrHelper
- Included in:
- DataPackage::Base
- Defined in:
- lib/attr_helper.rb,
lib/attr_helper/base_attr.rb,
lib/attr_helper/required_attr.rb,
lib/attr_helper/serialization.rb
Defined Under Namespace
Modules: ClassMethods, Serialization
Classes: BaseAttr, RequiredAttr
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(klass) ⇒ Object
6
7
8
|
# File 'lib/attr_helper.rb', line 6
def self.included(klass)
klass.send :extend, ClassMethods
end
|
Instance Method Details
#attr_missing?(name) ⇒ Boolean
86
87
88
|
# File 'lib/attr_helper.rb', line 86
def attr_missing?(name)
missing_attributes.any?{|a| a.name == name}
end
|
#attr_present?(name) ⇒ Boolean
90
91
92
|
# File 'lib/attr_helper.rb', line 90
def attr_present?(name)
!attr_missing?(name)
end
|
#attr_required?(name) ⇒ Boolean
82
83
84
|
# File 'lib/attr_helper.rb', line 82
def attr_required?(name)
required_attributes.any?{|a| a.name == name}
end
|
#attributes ⇒ Object
61
62
63
|
# File 'lib/attr_helper.rb', line 61
def attributes
required_attributes + optional_attributes
end
|
#missing_attributes ⇒ Object
75
76
77
78
79
80
|
# File 'lib/attr_helper.rb', line 75
def missing_attributes
required_attributes.select do |attribute|
value = send(attribute.name)
value.respond_to?(:empty?) ? value.empty? : value.nil?
end
end
|
#optional_attributes ⇒ Object
71
72
73
|
# File 'lib/attr_helper.rb', line 71
def optional_attributes
self.class.optional_attributes
end
|
#required_attributes ⇒ Object
65
66
67
68
69
|
# File 'lib/attr_helper.rb', line 65
def required_attributes
self.class.required_attributes.select do |attribute|
attribute.required?(self)
end
end
|
#write_attribute(name, value) ⇒ Object
94
95
96
|
# File 'lib/attr_helper.rb', line 94
def write_attribute(name, value)
self.instance_variable_set("@#{name}", value)
end
|
#write_attributes(attrs = {}) ⇒ Object
98
99
100
101
102
103
104
|
# File 'lib/attr_helper.rb', line 98
def write_attributes(attrs = {})
attrs = attrs.symbolize_keys
attributes.each do |attribute|
value = attrs[attribute.key.to_sym] || attribute.default
self.send("#{attribute.name}=", value) unless value.nil?
end
end
|