Class: Nanoc::Identifier

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/nanoc/base/entities/identifier.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Class Method Details

.from(obj) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/nanoc/base/entities/identifier.rb', line 42

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

Returns:

  • (String)


120
121
122
# File 'lib/nanoc/base/entities/identifier.rb', line 120

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

#chopString

Returns:

  • (String)


114
115
116
# File 'lib/nanoc/base/entities/identifier.rb', line 114

def chop
  to_s.chop
end

#componentsObject



183
184
185
186
187
188
189
190
# File 'lib/nanoc/base/entities/identifier.rb', line 183

def components
  res = to_s.split('/')
  if res.empty?
    []
  else
    res[1..-1]
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (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

#extObject

The extension, without a leading dot



151
152
153
154
155
156
157
158
# File 'lib/nanoc/base/entities/identifier.rb', line 151

def ext
  unless full?
    raise UnsupportedLegacyOperationError
  end

  s = File.extname(@string)
  s && s[1..-1]
end

#extsObject

The list of extensions, without a leading dot



173
174
175
176
177
178
179
180
# File 'lib/nanoc/base/entities/identifier.rb', line 173

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).

Returns:

  • (Boolean)


102
103
104
# File 'lib/nanoc/base/entities/identifier.rb', line 102

def full?
  @type == :full
end

#hashObject



86
87
88
# File 'lib/nanoc/base/entities/identifier.rb', line 86

def hash
  self.class.hash ^ to_s.hash
end

#inspectObject



203
204
205
# File 'lib/nanoc/base/entities/identifier.rb', line 203

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).

Returns:

  • (Boolean)


108
109
110
# File 'lib/nanoc/base/entities/identifier.rb', line 108

def legacy?
  @type == :legacy
end

#prefix(string) ⇒ Nanoc::Identifier

Returns:



126
127
128
129
130
131
# File 'lib/nanoc/base/entities/identifier.rb', line 126

def prefix(string)
  if string !~ /\A\//
    raise InvalidPrefixError.new(string)
  end
  Nanoc::Identifier.new(string.sub(/\/+\z/, '') + @string, type: @type)
end

#to_sObject



193
194
195
# File 'lib/nanoc/base/entities/identifier.rb', line 193

def to_s
  @string
end

#to_strObject



198
199
200
# File 'lib/nanoc/base/entities/identifier.rb', line 198

def to_str
  @string
end

#without_extObject

The identifier, as string, with the last extension removed



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/nanoc/base/entities/identifier.rb', line 135

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_extsObject

The identifier, as string, with all extensions removed



162
163
164
165
166
167
168
169
# File 'lib/nanoc/base/entities/identifier.rb', line 162

def without_exts
  extname = exts.join('.')
  if !extname.empty?
    @string[0..-extname.size - 2]
  else
    @string
  end
end