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

- (Identifier) initialize(string, type: :full)

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

+ (Object) from(obj)



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

- (String) +(other)

Returns:

  • (String)


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

def +(other)
  to_s + other
end

- (Object) <=>(other)



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

def <=>(other)
  to_s <=> other.to_s
end

- (Object) ==(other) 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

- (Object) =~(other)



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

- (String) chop

Returns:

  • (String)


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

def chop
  to_s.chop
end

- (Object) components



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

- (String) ext

Returns The extension, without a leading dot.

Returns:

  • (String)

    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

- (Array) exts

Returns List of extensions, without a leading dot.

Returns:

  • (Array)

    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

- (Boolean) full?

Returns True if this is a full-type identifier (i.e. includes the extension), false otherwise

Returns:

  • (Boolean)

    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

- (Object) hash



77
78
79
# File 'lib/nanoc/base/entities/identifier.rb', line 77

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

- (Object) inspect



181
182
183
# File 'lib/nanoc/base/entities/identifier.rb', line 181

def inspect
  "<Nanoc::Identifier type=#{@type} #{to_s.inspect}>"
end

- (Boolean) legacy?

Returns True if this is a legacy identifier (i.e. does not include the extension), false otherwise

Returns:

  • (Boolean)

    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

- (Nanoc::Identifier) prefix(string)

Returns:



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

- (Object) to_s



173
174
175
# File 'lib/nanoc/base/entities/identifier.rb', line 173

def to_s
  @string
end

- (Object) to_str



177
178
179
# File 'lib/nanoc/base/entities/identifier.rb', line 177

def to_str
  @string
end

- (String) without_ext

Returns:

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

- (String) without_exts

Returns:

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