Sha256: 659fd54256794f6afea9192d6de571738ca3ac006beade47c0087b351e7cbecb

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

require 'structure/class_methods'
require 'structure/utils'

module Structure
  def self.included(base)
    base.extend ClassMethods
    base.__overwrite_initialize__
  end

  def attributes
    attribute_names.each_with_object({}) do |key, hash|
      hash[key] = Utils.serialize(send(key))
    end
  end

  def attribute_names
    self.class.attribute_names
  end

  def ==(other)
    return false unless other.respond_to?(:attributes)

    attributes == other.attributes
  end

  def inspect
    name = self.class.name || self.class.to_s.gsub(/[^\w:]/, '')
    values =
      attribute_names
      .map do |key|
        value = send(key)
        if value.is_a?(::Array)
          description = value.take(3).map(&:inspect).join(', ')
          description += '...' if value.size > 3
          "#{key}=[#{description}]"
        else
          "#{key}=#{value.inspect}"
        end
      end
      .join(', ')

    "#<#{name} #{values}>"
  end

  alias to_h attributes
  alias eql? ==
  alias to_s inspect
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
structure-1.1.1 lib/structure.rb
structure-1.1.0 lib/structure.rb