Class: Utopia::Basename
- Inherits:
-
Object
- Object
- Utopia::Basename
- Defined in:
- lib/utopia/path.rb
Instance Attribute Summary collapse
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, extension = false) ⇒ Basename
constructor
A basename represents a file name with an optional extension.
- #parts ⇒ Object
- #rename(name) ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
- #variant ⇒ Object
Constructor Details
#initialize(name, extension = false) ⇒ Basename
A basename represents a file name with an optional extension. You can specify a specific extension to identify or specify true to select any extension after the last trailing dot.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/utopia/path.rb', line 24 def initialize(name, extension = false) if extension if extension == true offset = name.rindex('.') else offset = name.rindex(extension) - 1 end @name = name[0...offset] @extension = name[offset+1..-1] else @name = name @extension = nil end end |
Instance Attribute Details
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
49 50 51 |
# File 'lib/utopia/path.rb', line 49 def extension @extension end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
48 49 50 |
# File 'lib/utopia/path.rb', line 48 def name @name end |
Instance Method Details
#parts ⇒ Object
51 52 53 |
# File 'lib/utopia/path.rb', line 51 def parts @parts ||= @name.split('.') end |
#rename(name) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/utopia/path.rb', line 40 def rename(name) copy = self.dup copy.send(:instance_variable_set, :@name, name) return copy end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/utopia/path.rb', line 63 def to_s to_str end |
#to_str ⇒ Object
59 60 61 |
# File 'lib/utopia/path.rb', line 59 def to_str "#{name}#{extension}" end |
#variant ⇒ Object
55 56 57 |
# File 'lib/utopia/path.rb', line 55 def variant parts.last if parts.size > 1 end |