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.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# 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
|
Instance Method Details
129
130
131
|
# File 'lib/nanoc/core/identifier.rb', line 129
def +(other)
to_s + other
end
|
#<=>(other) ⇒ Object
105
106
107
|
# File 'lib/nanoc/core/identifier.rb', line 105
def <=>(other)
to_s <=> other.to_s
end
|
#==(other) ⇒ Object
75
76
77
78
79
80
81
82
|
# 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) ⇒ Object
95
96
97
|
# File 'lib/nanoc/core/identifier.rb', line 95
def =~(other)
Nanoc::Core::Pattern.from(other).match?(to_s) ? 0 : nil
end
|
123
124
125
|
# File 'lib/nanoc/core/identifier.rb', line 123
def chop
to_s.chop
end
|
#components ⇒ Object
193
194
195
196
197
198
199
200
|
# File 'lib/nanoc/core/identifier.rb', line 193
def components
res = to_s.split('/')
if res.empty?
[]
else
res[1..]
end
end
|
#eql?(other) ⇒ Boolean
85
86
87
|
# File 'lib/nanoc/core/identifier.rb', line 85
def eql?(other)
other.is_a?(self.class) && to_s == other.to_s
end
|
#ext ⇒ Object
The extension, without a leading dot
161
162
163
164
165
166
167
168
|
# File 'lib/nanoc/core/identifier.rb', line 161
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
183
184
185
186
187
188
189
190
|
# 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? ⇒ Boolean
Whether or not this is a full identifier (i.e.includes the extension).
111
112
113
|
# File 'lib/nanoc/core/identifier.rb', line 111
def full?
@type == :full
end
|
#hash ⇒ Object
90
91
92
|
# File 'lib/nanoc/core/identifier.rb', line 90
def hash
[self.class, to_s].hash
end
|
#inspect ⇒ Object
213
214
215
|
# File 'lib/nanoc/core/identifier.rb', line 213
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).
117
118
119
|
# File 'lib/nanoc/core/identifier.rb', line 117
def legacy?
@type == :legacy
end
|
#match?(other) ⇒ Boolean
100
101
102
|
# File 'lib/nanoc/core/identifier.rb', line 100
def match?(other)
Nanoc::Core::Pattern.from(other).match?(to_s)
end
|
135
136
137
138
139
140
141
|
# 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 ⇒ Object
203
204
205
|
# File 'lib/nanoc/core/identifier.rb', line 203
def to_s
@string
end
|
#to_str ⇒ Object
208
209
210
|
# File 'lib/nanoc/core/identifier.rb', line 208
def to_str
@string
end
|
#without_ext ⇒ Object
The identifier, as string, with the last extension removed
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/nanoc/core/identifier.rb', line 145
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
172
173
174
175
176
177
178
179
|
# File 'lib/nanoc/core/identifier.rb', line 172
def without_exts
extname = exts.join('.')
if extname.empty?
@string
else
@string[0..-extname.size - 2]
end
end
|