Class: Nanoc::Core::Identifier
- Inherits:
-
Object
- Object
- Nanoc::Core::Identifier
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
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
|
Instance Method Details
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
|
135
136
137
|
# File 'lib/nanoc/core/identifier.rb', line 135
def chop
to_s.chop
end
|
#components ⇒ Object
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
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
|
#ext ⇒ Object
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
|
#exts ⇒ Object
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).
122
123
124
|
# File 'lib/nanoc/core/identifier.rb', line 122
def full?
@type == :full
end
|
#hash ⇒ Object
101
102
103
|
# File 'lib/nanoc/core/identifier.rb', line 101
def hash
[self.class, to_s].hash
end
|
#inspect ⇒ Object
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).
129
130
131
|
# File 'lib/nanoc/core/identifier.rb', line 129
def legacy?
@type == :legacy
end
|
#match?(other) ⇒ Boolean
111
112
113
|
# File 'lib/nanoc/core/identifier.rb', line 111
def match?(other)
Nanoc::Core::Pattern.from(other).match?(to_s)
end
|
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_s ⇒ Object
218
219
220
|
# File 'lib/nanoc/core/identifier.rb', line 218
def to_s
@string
end
|
#to_str ⇒ Object
223
224
225
|
# File 'lib/nanoc/core/identifier.rb', line 223
def to_str
@string
end
|
#without_ext ⇒ Object
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_exts ⇒ Object
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
|