Module: AttrHelper::Base
- Included in:
- DataPackage::Base
- Defined in:
- lib/attr_helper/base.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #attr_missing?(name) ⇒ Boolean
- #attr_present?(name) ⇒ Boolean
- #attr_required?(name) ⇒ Boolean
- #attributes ⇒ Object
- #missing_attributes ⇒ Object
- #optional_attributes ⇒ Object
- #required_attributes ⇒ Object
- #write_attribute(name, value) ⇒ Object
- #write_attributes(attrs = {}) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
7 8 9 |
# File 'lib/attr_helper/base.rb', line 7 def self.included(klass) klass.send :extend, ClassMethods end |
Instance Method Details
#attr_missing?(name) ⇒ Boolean
97 98 99 |
# File 'lib/attr_helper/base.rb', line 97 def attr_missing?(name) missing_attributes.any?{|a| a.name == name} end |
#attr_present?(name) ⇒ Boolean
101 102 103 |
# File 'lib/attr_helper/base.rb', line 101 def attr_present?(name) !attr_missing?(name) end |
#attr_required?(name) ⇒ Boolean
93 94 95 |
# File 'lib/attr_helper/base.rb', line 93 def attr_required?(name) required_attributes.any?{|a| a.name == name} end |
#attributes ⇒ Object
80 81 82 |
# File 'lib/attr_helper/base.rb', line 80 def attributes required_attributes + optional_attributes end |
#missing_attributes ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/attr_helper/base.rb', line 84 def missing_attributes required_attributes.select do |attribute| attribute.required?(self) end.select do |attribute| value = send(attribute.name) value.respond_to?(:empty?) ? value.empty? : value.nil? end end |
#optional_attributes ⇒ Object
76 77 78 |
# File 'lib/attr_helper/base.rb', line 76 def optional_attributes self.class.optional_attributes end |
#required_attributes ⇒ Object
72 73 74 |
# File 'lib/attr_helper/base.rb', line 72 def required_attributes self.class.required_attributes end |
#write_attribute(name, value) ⇒ Object
105 106 107 |
# File 'lib/attr_helper/base.rb', line 105 def write_attribute(name, value) self.instance_variable_set("@#{name}", value) end |
#write_attributes(attrs = {}) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/attr_helper/base.rb', line 109 def write_attributes(attrs = {}) attrs = DataPackage::Helpers.symbolize_keys(attrs) attributes.each do |attribute| value = attrs[attribute.key.to_sym] || attribute.default self.send("#{attribute.name}=", value) unless value.nil? end end |