Class: Nanoc::Identifier

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/nanoc/base/entities/identifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, type: :full) ⇒ Identifier

Returns a new instance of Identifier



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nanoc/base/entities/identifier.rb', line 56

def initialize(string, type: :full)
  @type = type

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

Class Method Details

.from(obj) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/nanoc/base/entities/identifier.rb', line 44

def self.from(obj)
  case obj
  when Nanoc::Identifier
    obj
  when String
    Nanoc::Identifier.new(obj)
  else
    raise NonCoercibleObjectError.new(obj)
  end
end

Instance Method Details

#+(other) ⇒ String

Returns:

  • (String)


122
123
124
# File 'lib/nanoc/base/entities/identifier.rb', line 122

def +(other)
  to_s + other
end

#<=>(other) ⇒ Object



98
99
100
# File 'lib/nanoc/base/entities/identifier.rb', line 98

def <=>(other)
  to_s <=> other.to_s
end

#==(other) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/nanoc/base/entities/identifier.rb', line 73

def ==(other)
  case other
  when Nanoc::Identifier, String
    to_s == other.to_s
  else
    false
  end
end

#=~(other) ⇒ Object



93
94
95
# File 'lib/nanoc/base/entities/identifier.rb', line 93

def =~(other)
  Nanoc::Int::Pattern.from(other).match?(to_s) ? 0 : nil
end

#chopString

Returns:

  • (String)


116
117
118
# File 'lib/nanoc/base/entities/identifier.rb', line 116

def chop
  to_s.chop
end

#componentsObject



185
186
187
188
189
190
191
192
# File 'lib/nanoc/base/entities/identifier.rb', line 185

def components
  res = to_s.split('/')
  if res.empty?
    []
  else
    res[1..-1]
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/nanoc/base/entities/identifier.rb', line 83

def eql?(other)
  other.is_a?(self.class) && to_s == other.to_s
end

#extObject

The extension, without a leading dot



153
154
155
156
157
158
159
160
# File 'lib/nanoc/base/entities/identifier.rb', line 153

def ext
  unless full?
    raise UnsupportedLegacyOperationError
  end

  s = File.extname(@string)
  s && s[1..-1]
end

#extsObject

The list of extensions, without a leading dot



175
176
177
178
179
180
181
182
# File 'lib/nanoc/base/entities/identifier.rb', line 175

def exts
  unless full?
    raise UnsupportedLegacyOperationError
  end

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

#full?Boolean

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

Returns:

  • (Boolean)


104
105
106
# File 'lib/nanoc/base/entities/identifier.rb', line 104

def full?
  @type == :full
end

#hashObject



88
89
90
# File 'lib/nanoc/base/entities/identifier.rb', line 88

def hash
  self.class.hash ^ to_s.hash
end

#inspectObject



205
206
207
# File 'lib/nanoc/base/entities/identifier.rb', line 205

def inspect
  "<Nanoc::Identifier type=#{@type} #{to_s.inspect}>"
end

#legacy?Boolean

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

Returns:

  • (Boolean)


110
111
112
# File 'lib/nanoc/base/entities/identifier.rb', line 110

def legacy?
  @type == :legacy
end

#prefix(string) ⇒ Nanoc::Identifier

Returns:



128
129
130
131
132
133
# File 'lib/nanoc/base/entities/identifier.rb', line 128

def prefix(string)
  if string !~ /\A\//
    raise InvalidPrefixError.new(string)
  end
  Nanoc::Identifier.new(string.sub(/\/+\z/, '') + @string, type: @type)
end

#to_sObject



195
196
197
# File 'lib/nanoc/base/entities/identifier.rb', line 195

def to_s
  @string
end

#to_strObject



200
201
202
# File 'lib/nanoc/base/entities/identifier.rb', line 200

def to_str
  @string
end

#without_extObject

The identifier, as string, with the last extension removed



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nanoc/base/entities/identifier.rb', line 137

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_extsObject

The identifier, as string, with all extensions removed



164
165
166
167
168
169
170
171
# File 'lib/nanoc/base/entities/identifier.rb', line 164

def without_exts
  extname = exts.join('.')
  if !extname.empty?
    @string[0..-extname.size - 2]
  else
    @string
  end
end