module OptionsModel::Concerns::AttributeAssignment

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/options_model/concerns/attribute_assignment.rb, line 8
def initialize(attributes = {})
  update_attributes(attributes)
end

Public Instance Methods

[](key) click to toggle source
# File lib/options_model/concerns/attribute_assignment.rb, line 34
def [](key)
  public_send(key) if respond_to?(key)
end
[]=(key, val) click to toggle source
# File lib/options_model/concerns/attribute_assignment.rb, line 38
def []=(key, val)
  setter = "#{key}="
  if respond_to?(setter)
    public_send(setter, val)
  else
    unused_attributes[key] = val
  end
end
attributes() click to toggle source
# File lib/options_model/concerns/attribute_assignment.rb, line 60
def attributes
  @attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end
fetch(key, default = nil) { || ... } click to toggle source
# File lib/options_model/concerns/attribute_assignment.rb, line 47
def fetch(key, default = nil)
  raise KeyError, "attribute not found" if self.class.attribute_names.exclude?(key.to_sym) && default.nil? && !block_given?

  value = respond_to?(key) ? public_send(key) : nil
  return value if value

  if default
    default
  elsif block_given?
    yield
  end
end
initialize_dup(other) click to toggle source
Calls superclass method
# File lib/options_model/concerns/attribute_assignment.rb, line 12
def initialize_dup(other)
  super

  update_attributes(other)
end
nested_attributes() click to toggle source
# File lib/options_model/concerns/attribute_assignment.rb, line 64
def nested_attributes
  @nested_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end
replace(other) click to toggle source
# File lib/options_model/concerns/attribute_assignment.rb, line 18
def replace(other)
  return unless other

  raise ArgumentError, "#{other} must be respond to `to_h`" unless other.respond_to?(:to_h)

  other.to_h.each do |k, v|
    if respond_to?("#{k}=")
      public_send("#{k}=", v)
    else
      unused_attributes[k] = v
    end
  end
end
Also aliased as: update_attributes
unused_attributes() click to toggle source
# File lib/options_model/concerns/attribute_assignment.rb, line 68
def unused_attributes
  @unused_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end
update_attributes(other)
Alias for: replace