Sha256: 61f2870ca53a8540cc872a75bc68b44754fa8beb80293ba134c53e24c1abd477

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 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)
      Class.new(self) do
        include OptionsModel::Concerns::NameHacking
        self.name = name
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
options_model-0.0.9 lib/options_model/base.rb
options_model-0.0.8 lib/options_model/base.rb
options_model-0.0.7 lib/options_model/base.rb
options_model-0.0.6 lib/options_model/base.rb