Class: Nanoc::Identifier
Defined Under Namespace
Classes: InvalidIdentifierError, InvalidPrefixError, InvalidTypeError, NonCoercibleObjectError, UnsupportedLegacyOperationError
Class Method Summary
collapse
Instance Method Summary
collapse
included
Constructor Details
#initialize(string, type: :full) ⇒ Identifier
Returns a new instance of Identifier.
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/nanoc/base/entities/identifier.rb', line 54
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
|
Instance Method Details
122
123
124
|
# File 'lib/nanoc/base/entities/identifier.rb', line 122
def +(other)
to_s + other
end
|
#<=>(other) ⇒ Object
96
97
98
|
# File 'lib/nanoc/base/entities/identifier.rb', line 96
def <=>(other)
to_s <=> other.to_s
end
|
#==(other) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/nanoc/base/entities/identifier.rb', line 71
def ==(other)
case other
when Nanoc::Identifier, String
to_s == other.to_s
else
false
end
end
|
#=~(other) ⇒ Object
91
92
93
|
# File 'lib/nanoc/base/entities/identifier.rb', line 91
def =~(other)
Nanoc::Int::Pattern.from(other).match?(to_s) ? 0 : nil
end
|
116
117
118
|
# File 'lib/nanoc/base/entities/identifier.rb', line 116
def chop
to_s.chop
end
|
#components ⇒ Object
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
81
82
83
|
# File 'lib/nanoc/base/entities/identifier.rb', line 81
def eql?(other)
other.is_a?(self.class) && to_s == other.to_s
end
|
Returns 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
|
Returns 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
Returns True if this is a full-type identifier (i.e. includes the extension), false otherwise.
103
104
105
|
# File 'lib/nanoc/base/entities/identifier.rb', line 103
def full?
@type == :full
end
|
#hash ⇒ Object
86
87
88
|
# File 'lib/nanoc/base/entities/identifier.rb', line 86
def hash
self.class.hash ^ to_s.hash
end
|
#inspect ⇒ Object
205
206
207
|
# File 'lib/nanoc/base/entities/identifier.rb', line 205
def inspect
"<Nanoc::Identifier type=#{@type} #{to_s.inspect}>"
end
|
#legacy? ⇒ Boolean
Returns True if this is a legacy identifier (i.e. does not include the extension), false otherwise.
110
111
112
|
# File 'lib/nanoc/base/entities/identifier.rb', line 110
def legacy?
@type == :legacy
end
|
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_s ⇒ Object
195
196
197
|
# File 'lib/nanoc/base/entities/identifier.rb', line 195
def to_s
@string
end
|
#to_str ⇒ Object
200
201
202
|
# File 'lib/nanoc/base/entities/identifier.rb', line 200
def to_str
@string
end
|
#without_ext ⇒ String
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_exts ⇒ String
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
|