Sha256: 0f819b26d06362ed656a95b382778611fa0e726e7f0d18ddf787d2651aa2c41e

Contents?: true

Size: 1.83 KB

Versions: 56

Compression:

Stored size: 1.83 KB

Contents

module Her
  module Model
    module Introspection
      extend ActiveSupport::Concern
      # Inspect an element, returns it for introspection.
      #
      # @example
      #   class User
      #     include Her::Model
      #   end
      #
      #   @user = User.find(1)
      #   p @user # => #<User(/users/1) id=1 name="Tobias Fünke">
      def inspect
        resource_path = begin
          request_path
        rescue Her::Errors::PathError => e
          "<unknown path, missing `#{e.missing_parameter}`>"
        end

        "#<#{self.class}(#{resource_path}) #{attributes.keys.map { |k| "#{k}=#{attribute_for_inspect(send(k))}" }.join(" ")}>"
      end

      private
      def attribute_for_inspect(value)
        if value.is_a?(String) && value.length > 50
          "#{value[0..50]}...".inspect
        elsif value.is_a?(Date) || value.is_a?(Time)
          %("#{value}")
        else
          value.inspect
        end
      end

      # @private
      module ClassMethods
        # Finds a class at the same level as this one or at the global level.
        #
        # @private
        def her_nearby_class(name)
          her_sibling_class(name) || name.constantize rescue nil
        end

        protected
        # Looks for a class at the same level as this one with the given name.
        #
        # @private
        def her_sibling_class(name)
          if mod = self.her_containing_module
            @_her_sibling_class ||= Hash.new { Hash.new }
            @_her_sibling_class[mod][name] ||= "#{mod.name}::#{name}".constantize rescue nil
          end
        end

        # If available, returns the containing Module for this class.
        #
        # @private
        def her_containing_module
          return unless self.name =~ /::/
          self.name.split("::")[0..-2].join("::").constantize
        end
      end
    end
  end
end

Version data entries

56 entries across 56 versions & 5 rubygems

Version Path
test_track_rails_client-4.0.0.alpha12 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha11 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha10 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha9 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha8 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha7 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha6 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha5 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha4 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha3 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha2 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-4.0.0.alpha1 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-3.0.1 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-3.0.0 vendor/gems/her/lib/her/model/introspection.rb
her-0.10.0 lib/her/model/introspection.rb
test_track_rails_client-2.0.0 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-1.3.0 vendor/gems/her/lib/her/model/introspection.rb
test_track_rails_client-1.2.0 vendor/gems/her/lib/her/model/introspection.rb
her-0.9.0 lib/her/model/introspection.rb
test_track_rails_client-1.1.0 vendor/gems/her/lib/her/model/introspection.rb