Module: Chozo::VariaModel
- Included in:
- Config::Abstract
- Defined in:
- lib/chozo/varia_model.rb
Overview
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#_attributes_ ⇒ Hashie::Mash
(also: #to_hash)
The storage hash containing all of the key/values for this object’s attributes.
- #errors ⇒ HashWithIndifferentAccess
- #from_hash(hash) ⇒ self
- #from_json(data) ⇒ self
- #get_attribute(key) ⇒ Object (also: #[])
-
#mass_assign(new_attrs = {}) ⇒ Object
Assigns the attributes of a model from a given hash of attributes.
- #set_attribute(key, value) ⇒ Object (also: #[]=)
- #to_json(options = {}) ⇒ String (also: #as_json)
- #valid? ⇒ Boolean
- #validate ⇒ HashWithIndifferentAccess
Class Method Details
.included(base) ⇒ Object
158 159 160 |
# File 'lib/chozo/varia_model.rb', line 158 def included(base) base.extend(ClassMethods) end |
Instance Method Details
#_attributes_ ⇒ Hashie::Mash Also known as: to_hash
The storage hash containing all of the key/values for this object’s attributes
247 248 249 |
# File 'lib/chozo/varia_model.rb', line 247 def _attributes_ @_attributes_ ||= self.class.attributes.dup end |
#errors ⇒ HashWithIndifferentAccess
190 191 192 |
# File 'lib/chozo/varia_model.rb', line 190 def errors @errors ||= HashWithIndifferentAccess.new end |
#from_hash(hash) ⇒ self
231 232 233 234 |
# File 'lib/chozo/varia_model.rb', line 231 def from_hash(hash) mass_assign(hash.to_hash) self end |
#from_json(data) ⇒ self
239 240 241 242 |
# File 'lib/chozo/varia_model.rb', line 239 def from_json(data) mass_assign(MultiJson.decode(data)) self end |
#get_attribute(key) ⇒ Object Also known as: []
216 217 218 |
# File 'lib/chozo/varia_model.rb', line 216 def get_attribute(key) _attributes_.dig(key.to_s) end |
#mass_assign(new_attrs = {}) ⇒ Object
Assigns the attributes of a model from a given hash of attributes.
If the assignment mode is set to ‘:whitelist`, then only the values of keys which have a corresponding attribute definition on the model will be set. All other keys will have their values ignored.
If the assignment mode is set to ‘:carefree`, then the attributes hash will be populated with any key/values that are provided.
204 205 206 207 208 209 210 211 |
# File 'lib/chozo/varia_model.rb', line 204 def mass_assign(new_attrs = {}) case self.class.assignment_mode when :whitelist whitelist_assign(new_attrs) when :carefree carefree_assign(new_attrs) end end |
#set_attribute(key, value) ⇒ Object Also known as: []=
223 224 225 |
# File 'lib/chozo/varia_model.rb', line 223 def set_attribute(key, value) _attributes_.deep_merge!(Hashie::Mash.from_dotted_path(key.to_s, value)) end |
#to_json(options = {}) ⇒ String Also known as: as_json
256 257 258 |
# File 'lib/chozo/varia_model.rb', line 256 def to_json( = {}) MultiJson.encode(_attributes_, ) end |
#valid? ⇒ Boolean
185 186 187 |
# File 'lib/chozo/varia_model.rb', line 185 def valid? validate.empty? end |
#validate ⇒ HashWithIndifferentAccess
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/chozo/varia_model.rb', line 164 def validate self.class.validations.each do |attr_path, validations| validations.each do |validation| status, = validation.call(self, attr_path) if status == :error if .is_a?(Array) .each do || self.add_error(attr_path, ) end else self.add_error(attr_path, ) end end end end self.errors end |