module Commutator::Model::Attributes

This module provides methods related to registering attributes and generating attr_accessors, ultimately for the purpose of enabling Commutator::Model to know which attributes to send to the db.

Public Instance Methods

assign_attributes(attrs = {}) click to toggle source
# File lib/commutator/model/attributes.rb, line 9
def assign_attributes(attrs = {})
  attrs.slice(*attribute_names).each do |attr_name, value|
    send("#{attr_name}=", value)
  end
end
attribute_names() click to toggle source
# File lib/commutator/model/attributes.rb, line 21
def attribute_names
  self.class.attribute_names
end
attributes() click to toggle source
# File lib/commutator/model/attributes.rb, line 15
def attributes
  attribute_names.each_with_object({}) do |attr_name, hash|
    hash[attr_name] = send(attr_name)
  end
end

Private Instance Methods

convert_type(value, options) click to toggle source
# File lib/commutator/model/attributes.rb, line 27
def convert_type(value, options)
  return if options.fetch(:allow_nil, true) && value.nil?

  case options[:type]
  when :array then value.to_a
  when :boolean then !!value
  when :float then value.to_f
  when :hash then value.to_h
  when :integer then value.to_i
  when :set then Set.new(value)
  when :string then value.to_s
  else value
  end
end