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
(also: #eql?)
-
#=~(other) ⇒ Object
-
#chop ⇒ String
-
#components ⇒ Object
-
#ext ⇒ String
The extension, without a leading dot.
-
#exts ⇒ Array
List of extensions, without a leading dot.
-
#full? ⇒ Boolean
True if this is a full-type identifier (i.e. includes the extension), false otherwise.
-
#hash ⇒ Object
-
#initialize(string, type: :full) ⇒ Identifier
constructor
A new instance of Identifier.
-
#inspect ⇒ Object
-
#legacy? ⇒ Boolean
True if this is a legacy identifier (i.e. does not include the extension), false otherwise.
-
#prefix(string) ⇒ Nanoc::Identifier
-
#to_s ⇒ Object
-
#to_str ⇒ Object
-
#without_ext ⇒ String
-
#without_exts ⇒ String
Constructor Details
#initialize(string, type: :full) ⇒ Identifier
Returns a new instance of Identifier
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/nanoc/base/entities/identifier.rb', line 51 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 |
Class Method Details
.from(obj) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/nanoc/base/entities/identifier.rb', line 40 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
107 108 109 |
# File 'lib/nanoc/base/entities/identifier.rb', line 107 def +(other) to_s + other end |
#<=>(other) ⇒ Object
85 86 87 |
# File 'lib/nanoc/base/entities/identifier.rb', line 85 def <=>(other) to_s <=> other.to_s end |
#==(other) ⇒ Object Also known as: eql?
67 68 69 70 71 72 73 74 |
# File 'lib/nanoc/base/entities/identifier.rb', line 67 def ==(other) case other when Nanoc::Identifier, String to_s == other.to_s else false end end |
#=~(other) ⇒ Object
81 82 83 |
# File 'lib/nanoc/base/entities/identifier.rb', line 81 def =~(other) Nanoc::Int::Pattern.from(other).match?(to_s) ? 0 : nil end |
#chop ⇒ String
102 103 104 |
# File 'lib/nanoc/base/entities/identifier.rb', line 102 def chop to_s.chop end |
#components ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/nanoc/base/entities/identifier.rb', line 164 def components res = to_s.split('/') if res.empty? [] else res[1..-1] end end |
#ext ⇒ String
Returns The extension, without a leading dot.
135 136 137 138 139 140 141 142 |
# File 'lib/nanoc/base/entities/identifier.rb', line 135 def ext unless full? raise UnsupportedLegacyOperationError end s = File.extname(@string) s && s[1..-1] end |
#exts ⇒ Array
Returns List of extensions, without a leading dot.
155 156 157 158 159 160 161 162 |
# File 'lib/nanoc/base/entities/identifier.rb', line 155 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
91 92 93 |
# File 'lib/nanoc/base/entities/identifier.rb', line 91 def full? @type == :full end |
#hash ⇒ Object
77 78 79 |
# File 'lib/nanoc/base/entities/identifier.rb', line 77 def hash self.class.hash ^ to_s.hash end |
#inspect ⇒ Object
181 182 183 |
# File 'lib/nanoc/base/entities/identifier.rb', line 181 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
97 98 99 |
# File 'lib/nanoc/base/entities/identifier.rb', line 97 def legacy? @type == :legacy end |
#prefix(string) ⇒ Nanoc::Identifier
112 113 114 115 116 117 |
# File 'lib/nanoc/base/entities/identifier.rb', line 112 def prefix(string) if string !~ /\A\// raise InvalidPrefixError.new(@string) end Nanoc::Identifier.new(string.sub(/\/+\z/, '') + @string, type: @type) end |
#to_s ⇒ Object
173 174 175 |
# File 'lib/nanoc/base/entities/identifier.rb', line 173 def to_s @string end |
#to_str ⇒ Object
177 178 179 |
# File 'lib/nanoc/base/entities/identifier.rb', line 177 def to_str @string end |
#without_ext ⇒ String
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/nanoc/base/entities/identifier.rb', line 120 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
145 146 147 148 149 150 151 152 |
# File 'lib/nanoc/base/entities/identifier.rb', line 145 def without_exts extname = exts.join('.') if !extname.empty? @string[0..-extname.size - 2] else @string end end |