Class: Nanoc::Identifier
- Inherits:
-
Object
- Object
- Nanoc::Identifier
- Includes:
- Comparable
- Defined in:
- lib/nanoc/base/entities/identifier.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#+(other) ⇒ String
-
#<=>(other) ⇒ Object
-
#==(other) ⇒ Object
-
#=~(other) ⇒ Object
-
#chop ⇒ String
-
#components ⇒ Object
-
#eql?(other) ⇒ Boolean
-
#ext ⇒ Object
The extension, without a leading dot.
-
#exts ⇒ Object
The list of extensions, without a leading dot.
-
#full? ⇒ Boolean
Whether or not this is a full identifier (i.e.includes the extension).
-
#hash ⇒ Object
-
#initialize(string, type: :full) ⇒ Identifier
constructor
A new instance of Identifier.
-
#inspect ⇒ Object
-
#legacy? ⇒ Boolean
Whether or not this is a legacy identifier (i.e. does not include the extension).
-
#prefix(string) ⇒ Nanoc::Identifier
-
#to_s ⇒ Object
-
#to_str ⇒ Object
-
#without_ext ⇒ Object
The identifier, as string, with the last extension removed.
-
#without_exts ⇒ Object
The identifier, as string, with all extensions removed.
Constructor Details
#initialize(string, type: :full) ⇒ Identifier
Returns a new instance of Identifier
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/nanoc/base/entities/identifier.rb', line 63 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 |
Class Method Details
.from(obj) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/nanoc/base/entities/identifier.rb', line 51 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
129 130 131 |
# File 'lib/nanoc/base/entities/identifier.rb', line 129 def +(other) to_s + other end |
#<=>(other) ⇒ Object
105 106 107 |
# File 'lib/nanoc/base/entities/identifier.rb', line 105 def <=>(other) to_s <=> other.to_s end |
#==(other) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/nanoc/base/entities/identifier.rb', line 80 def ==(other) case other when Nanoc::Identifier, String to_s == other.to_s else false end end |
#=~(other) ⇒ Object
100 101 102 |
# File 'lib/nanoc/base/entities/identifier.rb', line 100 def =~(other) Nanoc::Int::Pattern.from(other).match?(to_s) ? 0 : nil end |
#chop ⇒ String
123 124 125 |
# File 'lib/nanoc/base/entities/identifier.rb', line 123 def chop to_s.chop end |
#components ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/nanoc/base/entities/identifier.rb', line 192 def components res = to_s.split('/') if res.empty? [] else res[1..-1] end end |
#eql?(other) ⇒ Boolean
90 91 92 |
# File 'lib/nanoc/base/entities/identifier.rb', line 90 def eql?(other) other.is_a?(self.class) && to_s == other.to_s end |
#ext ⇒ Object
The extension, without a leading dot
160 161 162 163 164 165 166 167 |
# File 'lib/nanoc/base/entities/identifier.rb', line 160 def ext unless full? raise UnsupportedLegacyOperationError end s = File.extname(@string) s && s[1..-1] end |
#exts ⇒ Object
The list of extensions, without a leading dot
182 183 184 185 186 187 188 189 |
# File 'lib/nanoc/base/entities/identifier.rb', line 182 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/base/entities/identifier.rb', line 111 def full? @type == :full end |
#hash ⇒ Object
95 96 97 |
# File 'lib/nanoc/base/entities/identifier.rb', line 95 def hash self.class.hash ^ to_s.hash end |
#inspect ⇒ Object
212 213 214 |
# File 'lib/nanoc/base/entities/identifier.rb', line 212 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).
117 118 119 |
# File 'lib/nanoc/base/entities/identifier.rb', line 117 def legacy? @type == :legacy end |
#prefix(string) ⇒ Nanoc::Identifier
135 136 137 138 139 140 |
# File 'lib/nanoc/base/entities/identifier.rb', line 135 def prefix(string) if string !~ /\A\// raise InvalidPrefixError.new(string) end Nanoc::Identifier.new(string.sub(/\/+\z/, '') + @string, type: @type) end |
#to_s ⇒ Object
202 203 204 |
# File 'lib/nanoc/base/entities/identifier.rb', line 202 def to_s @string end |
#to_str ⇒ Object
207 208 209 |
# File 'lib/nanoc/base/entities/identifier.rb', line 207 def to_str @string end |
#without_ext ⇒ Object
The identifier, as string, with the last extension removed
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/nanoc/base/entities/identifier.rb', line 144 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 ⇒ Object
The identifier, as string, with all extensions removed
171 172 173 174 175 176 177 178 |
# File 'lib/nanoc/base/entities/identifier.rb', line 171 def without_exts extname = exts.join('.') if !extname.empty? @string[0..-extname.size - 2] else @string end end |