class Nanoc::Core::Identifier

Public Class Methods

from(obj) click to toggle source
# File lib/nanoc/core/identifier.rb, line 46
def self.from(obj)
  case obj
  when Nanoc::Core::Identifier
    obj
  when String
    Nanoc::Core::Identifier.new(obj)
  else
    raise NonCoercibleObjectError.new(obj)
  end
end
new(string, type: :full) click to toggle source
# File lib/nanoc/core/identifier.rb, line 58
def initialize(string, type: :full)
  @type = type

  case @type
  when :legacy
    @string = "/#{string}/".gsub(/^\/+|\/+$/, '/').freeze
  when :full
    raise InvalidIdentifierError.new(string) if string !~ /\A\//
    raise InvalidFullIdentifierError.new(string) if string =~ /\/\z/

    @string = string.dup.freeze
  else
    raise InvalidTypeError.new(@type)
  end
end

Public Instance Methods

+(other) click to toggle source

@return [String]

# File lib/nanoc/core/identifier.rb, line 129
def +(other)
  to_s + other
end
<=>(other) click to toggle source
# File lib/nanoc/core/identifier.rb, line 105
def <=>(other)
  to_s <=> other.to_s
end
==(other) click to toggle source
# File lib/nanoc/core/identifier.rb, line 75
def ==(other)
  case other
  when Nanoc::Core::Identifier, String
    to_s == other.to_s
  else
    false
  end
end
=~(other) click to toggle source
# File lib/nanoc/core/identifier.rb, line 95
def =~(other)
  Nanoc::Core::Pattern.from(other).match?(to_s) ? 0 : nil
end
chop() click to toggle source

@return [String]

# File lib/nanoc/core/identifier.rb, line 123
def chop
  to_s.chop
end
components() click to toggle source
# File lib/nanoc/core/identifier.rb, line 193
def components
  res = to_s.split('/')
  if res.empty?
    []
  else
    res[1..-1]
  end
end
eql?(other) click to toggle source
# File lib/nanoc/core/identifier.rb, line 85
def eql?(other)
  other.is_a?(self.class) && to_s == other.to_s
end
ext() click to toggle source

The extension, without a leading dot

# File lib/nanoc/core/identifier.rb, line 161
def ext
  unless full?
    raise UnsupportedLegacyOperationError
  end

  s = File.extname(@string)
  s && s[1..-1]
end
exts() click to toggle source

The list of extensions, without a leading dot

# File lib/nanoc/core/identifier.rb, line 183
def exts
  unless full?
    raise UnsupportedLegacyOperationError
  end

  s = File.basename(@string)
  s ? s.split('.', -1).drop(1) : []
end
full?() click to toggle source

Whether or not this is a full identifier (i.e.includes the extension).

# File lib/nanoc/core/identifier.rb, line 111
def full?
  @type == :full
end
hash() click to toggle source
# File lib/nanoc/core/identifier.rb, line 90
def hash
  self.class.hash ^ to_s.hash
end
inspect() click to toggle source
# File lib/nanoc/core/identifier.rb, line 213
def inspect
  "<Nanoc::Core::Identifier type=#{@type} #{to_s.inspect}>"
end
legacy?() click to toggle source

Whether or not this is a legacy identifier (i.e. does not include the extension).

# File lib/nanoc/core/identifier.rb, line 117
def legacy?
  @type == :legacy
end
match?(other) click to toggle source
# File lib/nanoc/core/identifier.rb, line 100
def match?(other)
  Nanoc::Core::Pattern.from(other).match?(to_s)
end
prefix(string) click to toggle source

@return [Nanoc::Core::Identifier]

# File lib/nanoc/core/identifier.rb, line 135
def prefix(string)
  unless /\A\//.match?(string)
    raise InvalidPrefixError.new(string)
  end

  Nanoc::Core::Identifier.new(string.sub(/\/+\z/, '') + @string, type: @type)
end
to_s() click to toggle source
# File lib/nanoc/core/identifier.rb, line 203
def to_s
  @string
end
to_str() click to toggle source
# File lib/nanoc/core/identifier.rb, line 208
def to_str
  @string
end
without_ext() click to toggle source

The identifier, as string, with the last extension removed

# File lib/nanoc/core/identifier.rb, line 145
def without_ext
  unless full?
    raise UnsupportedLegacyOperationError
  end

  extname = File.extname(@string)

  if !extname.empty?
    @string[0..-extname.size - 1]
  else
    @string
  end
end
without_exts() click to toggle source

The identifier, as string, with all extensions removed

# File lib/nanoc/core/identifier.rb, line 172
def without_exts
  extname = exts.join('.')
  if !extname.empty?
    @string[0..-extname.size - 2]
  else
    @string
  end
end