Module: Chozo::VariaModel

Included in:
Config::Abstract
Defined in:
lib/chozo/varia_model.rb

Overview

Author:

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:



247
248
249
# File 'lib/chozo/varia_model.rb', line 247

def _attributes_
  @_attributes_ ||= self.class.attributes.dup
end

#errorsHashWithIndifferentAccess

Returns:

  • (HashWithIndifferentAccess)


190
191
192
# File 'lib/chozo/varia_model.rb', line 190

def errors
  @errors ||= HashWithIndifferentAccess.new
end

#from_hash(hash) ⇒ self

Parameters:

Returns:

  • (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

Parameters:

  • data (String)

Returns:

  • (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: []

Parameters:

  • key (#to_s)

Returns:



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.

Parameters:

  • new_attrs (Hash) (defaults to: {})


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: []=

Parameters:



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

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :symbolize_keys (Boolean)
  • :adapter (Class, Symbol, String)

Returns:

  • (String)


256
257
258
# File 'lib/chozo/varia_model.rb', line 256

def to_json(options = {})
  MultiJson.encode(_attributes_, options)
end

#valid?Boolean

Returns:



185
186
187
# File 'lib/chozo/varia_model.rb', line 185

def valid?
  validate.empty?
end

#validateHashWithIndifferentAccess

Returns:

  • (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, messages = validation.call(self, attr_path)

      if status == :error
        if messages.is_a?(Array)
          messages.each do |message|
            self.add_error(attr_path, message)
          end
        else
          self.add_error(attr_path, messages)
        end
      end
    end
  end

  self.errors
end