Module: NinjaModel::AttributeMethods
- Extended by:
- ActiveSupport::Concern
- Included in:
- Base
- Defined in:
- lib/ninja_model/attribute_methods.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #[](attr_name) ⇒ Object
- #[]=(attr_name, value) ⇒ Object
- #attributes=(new_attributes) ⇒ Object
- #attributes_from_model_attributes ⇒ Object
- #read_attribute(name) ⇒ Object
- #write_attribute(name, value) ⇒ Object
Instance Method Details
#[](attr_name) ⇒ Object
86 87 88 |
# File 'lib/ninja_model/attribute_methods.rb', line 86 def [](attr_name) read_attribute(attr_name) end |
#[]=(attr_name, value) ⇒ Object
90 91 92 |
# File 'lib/ninja_model/attribute_methods.rb', line 90 def []=(attr_name, value) write_attribute(attr_name, value) end |
#attributes=(new_attributes) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ninja_model/attribute_methods.rb', line 94 def attributes=(new_attributes) return unless new_attributes.is_a?(Hash) attributes = new_attributes.stringify_keys multi_parameter_attributes = [] attributes.each do |k,v| if k.include?('(') multi_parameter_attributes << [k, v] else respond_to?("#{k}=".to_sym) ? send("#{k}=".to_sym, v) : raise(NinjaModel::UnknownAttributeError, "unknown attribute: #{k}") end end assign_multiparameter_attributes(multi_parameter_attributes) end |
#attributes_from_model_attributes ⇒ Object
61 62 63 64 65 66 |
# File 'lib/ninja_model/attribute_methods.rb', line 61 def attributes_from_model_attributes self.class.model_attributes.inject({}) do |result, attr| result[attr.name] = attr.default unless attr.name == self.class.primary_key result end end |
#read_attribute(name) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/ninja_model/attribute_methods.rb', line 77 def read_attribute(name) name = name.to_s if !(value = @attributes[name]).nil? self.class.model_attributes_hash[name].convert(@attributes[name]) else nil end end |
#write_attribute(name, value) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/ninja_model/attribute_methods.rb', line 68 def write_attribute(name, value) name = name.to_s if a = self.class.model_attributes_hash[name] @attributes[name] = value else raise NoMethodError, "Unknown attribute #{name.inspect}" end end |