Sha256: 7b33c6e13260d43f219712d339f92de7ca84d348c1f800f8065525d06a04b383

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module OptionsModel
  class Base
    include ActiveModel::Model
    include OptionsModel::Concerns::Serialization
    include OptionsModel::Concerns::Attributes
    include OptionsModel::Concerns::AttributeAssignment

    validate do
      self.class.attribute_names.each do |attribute_name|
        attribute = public_send(attribute_name)
        if attribute.is_a?(self.class) && attribute.invalid?
          errors.add attribute_name, :invalid
        end
      end
    end

    def ==(other)
      other.instance_of?(self.class) &&
        attributes == other.attributes &&
        nested_attributes == other.nested_attributes &&
        unused_attributes == other.unused_attributes
    end
    alias :eql? :==

    def hash
      [attributes, nested_attributes, unused_attributes].hash
    end

    def inspect
      "#<#{self.class.name}:OptionsModel #{self.to_h}>"
    end

    def self.inspect
      "#<#{name}:OptionsModel [#{attribute_names.map(&:inspect).join(', ')}]>"
    end

    def persisted?
      true
    end

    def self.derive(name = "AnonymousRecord")
      Class.new(self) do
        include OptionsModel::Concerns::NameHacking
        self.name = name
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
options_model-0.0.12 lib/options_model/base.rb
options_model-0.0.11 lib/options_model/base.rb
options_model-0.0.10 lib/options_model/base.rb