Class: Nanoc::Core::Identifier

Inherits:
Object
  • Object
show all
Includes:
Comparable, ContractsSupport
Defined in:
lib/nanoc/core/identifier.rb

Defined Under Namespace

Classes: InvalidFullIdentifierError, InvalidIdentifierError, InvalidPrefixError, InvalidTypeError, NonCoercibleObjectError, UnsupportedLegacyOperationError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Constructor Details

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

Returns a new instance of Identifier.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nanoc/core/identifier.rb', line 69

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

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

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

Class Method Details

.from(obj) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/nanoc/core/identifier.rb', line 57

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

Instance Method Details

#+(other) ⇒ String

Returns:



141
142
143
# File 'lib/nanoc/core/identifier.rb', line 141

def +(other)
  to_s + other
end

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/nanoc/core/identifier.rb', line 86

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

#=~(other) ⇒ Object



106
107
108
# File 'lib/nanoc/core/identifier.rb', line 106

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

#chopString

Returns:



135
136
137
# File 'lib/nanoc/core/identifier.rb', line 135

def chop
  to_s.chop
end

#componentsObject



208
209
210
211
212
213
214
215
# File 'lib/nanoc/core/identifier.rb', line 208

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/nanoc/core/identifier.rb', line 96

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

#extObject

The extension, without a leading dot



176
177
178
179
180
181
182
183
# File 'lib/nanoc/core/identifier.rb', line 176

def ext
  unless full?
    raise UnsupportedLegacyOperationError
  end

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

#extsObject

The list of extensions, without a leading dot



198
199
200
201
202
203
204
205
# File 'lib/nanoc/core/identifier.rb', line 198

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)


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

def full?
  @type == :full
end

#hashObject



101
102
103
# File 'lib/nanoc/core/identifier.rb', line 101

def hash
  [self.class, to_s].hash
end

#inspectObject



228
229
230
# File 'lib/nanoc/core/identifier.rb', line 228

def inspect
  "<Nanoc::Core::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)


129
130
131
# File 'lib/nanoc/core/identifier.rb', line 129

def legacy?
  @type == :legacy
end

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/nanoc/core/identifier.rb', line 111

def match?(other)
  Nanoc::Core::Pattern.from(other).match?(to_s)
end

#prefix(string) ⇒ Nanoc::Core::Identifier



147
148
149
150
151
152
153
154
155
156
# File 'lib/nanoc/core/identifier.rb', line 147

def prefix(string)
  unless %r{\A/}.match?(string)
    raise InvalidPrefixError.new(string)
  end

  Nanoc::Core::Identifier.new(
    string.sub(%r{/+\z}, '') + @string,
    type: @type,
  )
end

#to_sObject



218
219
220
# File 'lib/nanoc/core/identifier.rb', line 218

def to_s
  @string
end

#to_strObject



223
224
225
# File 'lib/nanoc/core/identifier.rb', line 223

def to_str
  @string
end

#without_extObject

The identifier, as string, with the last extension removed



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/nanoc/core/identifier.rb', line 160

def without_ext
  unless full?
    raise UnsupportedLegacyOperationError
  end

  extname = File.extname(@string)

  if extname.empty?
    @string
  else
    @string[0..(-extname.size - 1)]
  end
end

#without_extsObject

The identifier, as string, with all extensions removed



187
188
189
190
191
192
193
194
# File 'lib/nanoc/core/identifier.rb', line 187

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