Sha256: b0c356d59e5fe5a905d152a94c2d8acab99c2022f93f450d32932936b9ee864e

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 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
        "#<#{self.class}(#{request_path}) #{@data.keys.map { |k| "#{k}=#{attribute_for_inspect(send(k))}" }.join(" ")}>"
      end

      private
      # @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

      module ClassMethods
        # Finds a class at the same level as this one or at the global level.
        # @private
        def nearby_class(name)
          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 sibling_class(name)
          if mod = self.containing_module
            @sibling_class ||= {}
            @sibling_class[mod] ||= {}
            @sibling_class[mod][name] ||= "#{mod.name}::#{name}".constantize rescue nil
          end
        end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
her-0.5.1 lib/her/model/introspection.rb
her-0.5 lib/her/model/introspection.rb
her-0.4.1 lib/her/model/introspection.rb
her-0.4 lib/her/model/introspection.rb
her-0.3.8 lib/her/model/introspection.rb