Sha256: 09eb077839c69c77f396b0db6ca91920a3f13c2bf7c37768a7fa65b37efe08f2

Contents?: true

Size: 824 Bytes

Versions: 2

Compression:

Stored size: 824 Bytes

Contents

# frozen_string_literal: true

module Wayfarer
  module Stringify
    def self.included(base)
      base.include(InstanceMethods)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def stringify(*variables)
        stringified_attributes.concat(variables)
      end

      def stringified_attributes
        @stringified_attributes ||= []
      end
    end

    module InstanceMethods
      def to_s
        "#<#{class_name} #{attributes}>"
      end

      alias inspect to_s

      def class_name
        self.class.name.split("::").last
      end

      def attributes
        self.class
            .stringified_attributes
            .map { |attr| [attr, public_send(attr)] }
            .to_h
            .map { |k, v| [k, "=", v.inspect].join }
            .join(", ")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wayfarer-0.4.1 lib/wayfarer/stringify.rb
wayfarer-0.4.0 lib/wayfarer/stringify.rb