Sha256: c24d9e326ce734ed5cc81c922872a82aa6a1f3f6cfea151fad884cf83ac2bd70

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Reality
  class Link
    attr_reader :source, :id, :title

    def initialize(source, id, title: nil)
      @source = source
      @id = id
      @title = title || id
    end

    def inspect
      if title && title != id
        '#<%s %s://%s (%s)>' % [self.class, pretty_source, id, title]
      else
        '#<%s %s://%s>' % [self.class, pretty_source, id]
      end
    end

    def to_s
      if title && title != id
        '<%s://%s (%s)>' % [pretty_source, id, title]
      else
        '<%s://%s>' % [pretty_source, id]
      end
    end

    def pretty_source
      known = known_source? ? '' : '?'
      "#{source}#{known}"
    end

    def known_source?
      Reality.describers.key?(source)
    end

    def ==(other)
      other.is_a?(Link) && other.source == source && other.id == id
    end

    alias eql? ==

    def hash
      [self.class, source, id].hash
    end

    def load
      known_source? or fail %{Reality describer "#{source}" is yet to be implemented}
      Reality.describers.fetch(source).get(id)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reality-0.1.0.alpha3 lib/reality/link.rb
reality-0.1.0.alpha2 lib/reality/link.rb
reality-0.1.0.alpha lib/reality/link.rb