Sha256: 721025ab81d3b0a45b231f82b89889030c0591f5949b012090dc55175039487f

Contents?: true

Size: 1.52 KB

Versions: 21

Compression:

Stored size: 1.52 KB

Contents

module Gecko
  module Helpers
    # Helper for providing custom #inspect values to Records
    module InspectionHelper
      # Overrides the default inspect to just return the defined attributes
      #
      # @example
      #   company.inspect #=> <Gecko::Record::Company id=123 name="Gecko Inc">
      #
      # @return [String]
      #
      # @api public
      def inspect
        inspection = self.class.attribute_set.map do |attribute|
          name = attribute.name
          "#{name}: #{attribute_for_inspect(name)}"
        end.join(', ')
        "#<#{self.class} #{inspection}>"
      end

    private

      # Returns an #inspect-like string for the value of the attribute
      # attr_name.
      # String attributes are truncated up to 50 characters,
      # and Date and Time attributes are returned in the :db format.
      # Other attributes return the value of #inspect without modification.
      # Duplicated from Rails implementation
      #
      # @example
      #   company.inspect #=> <Gecko::Record::Company id=123 name="Gecko Inc">
      #
      # @return [String]
      #
      # @api private
      def attribute_for_inspect(attr_name)
        value = attributes[attr_name]

        if value.is_a?(String) && value.length > 50
          "#{value[0..50]}...".inspect
        elsif value.is_a?(DateTime) || value.is_a?(Time)
          %("#{value.strftime("%Y-%m-%d %H:%M:%S")}")
        elsif value.is_a?(Date)
          %("#{value.strftime("%Y-%m-%d")}")
        else
          value.inspect
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
gecko-ruby-0.9.1 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.9.0 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.8.0 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.2.6 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.7.1 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.7.0 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.6.0 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.5.0 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.2.5 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.2.4 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.2.3 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.2.2 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.2.0 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.1.0 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.0.10 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.0.9 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.0.8 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.0.7 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.0.6 lib/gecko/helpers/inspection_helper.rb
gecko-ruby-0.0.5 lib/gecko/helpers/inspection_helper.rb