Sha256: 209f7394bbb462136e7b1dbfaa467d46f27226c1f1eb76816b20a454eba6a5f8

Contents?: true

Size: 1.63 KB

Versions: 6

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module OptionsModel
  module Concerns
    module AttributeAssignment
      extend ActiveSupport::Concern

      def initialize(attributes = {})
        update_attributes(attributes)
      end

      def initialize_dup(other)
        super

        update_attributes(other)
      end

      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

      alias update_attributes replace

      def [](key)
        public_send(key) if respond_to?(key)
      end

      def []=(key, val)
        setter = "#{key}="
        if respond_to?(setter)
          public_send(setter, val)
        else
          unused_attributes[key] = val
        end
      end

      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

      def attributes
        @attributes ||= ActiveSupport::HashWithIndifferentAccess.new
      end

      def nested_attributes
        @nested_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
      end

      def unused_attributes
        @unused_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
options_model-0.0.19 lib/options_model/concerns/attribute_assignment.rb
options_model-0.0.18 lib/options_model/concerns/attribute_assignment.rb
options_model-0.0.17 lib/options_model/concerns/attribute_assignment.rb
options_model-0.0.16 lib/options_model/concerns/attribute_assignment.rb
options_model-0.0.15 lib/options_model/concerns/attribute_assignment.rb
options_model-0.0.14 lib/options_model/concerns/attribute_assignment.rb