Class: RubyTDMS::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_tdms/path.rb

Constant Summary collapse

PATH_MATCHER =
/(?:(?<!\\))\//
RAW_MATCHER =
/(?:^|(?<='))\/(?:(?=')|$)/

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Path

Can initialize with parts, path, or raw. Elements can contain / only in raw or parts forms.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/ruby_tdms/path.rb', line 7

def initialize(options = {})
	raise ArgumentError, 'Initialize with at most one of +parts+, +path+, or +raw+.' if options.length > 1
	@parts = options[:parts] || []
	self.path = options[:path] if options.has_key? :path
	self.raw = options[:raw] if options.has_key? :raw
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



65
66
67
68
69
70
71
72
73
# File 'lib/ruby_tdms/path.rb', line 65

def ==(other)
	if other.is_a? String
		self.to_s == other
	elsif other.is_a? self.class
		self.dump == other.dump
	else
		super
	end
end

#dumpObject



15
16
17
# File 'lib/ruby_tdms/path.rb', line 15

def dump
	to_s
end

#hashObject



20
21
22
# File 'lib/ruby_tdms/path.rb', line 20

def hash
	to_s.hash
end

#inspectObject



25
26
27
# File 'lib/ruby_tdms/path.rb', line 25

def inspect
	"#<#{self.class.name}:#{self.object_id} path=#{path.inspect}>"
end

#load(string) ⇒ Object



30
31
32
# File 'lib/ruby_tdms/path.rb', line 30

def load(string)
	self.path = string
end

#pathObject



35
36
37
# File 'lib/ruby_tdms/path.rb', line 35

def path
	'/' + @parts.map { |part| part.gsub('/', '\/') }.join('/')
end

#path=(value) ⇒ Object



40
41
42
# File 'lib/ruby_tdms/path.rb', line 40

def path=(value)
	@parts = value._?('').split(PATH_MATCHER).reject { |x| x.length == 0 }.map { |part| decode_part part }
end

#rawObject



45
46
47
# File 'lib/ruby_tdms/path.rb', line 45

def raw
	'/' + @parts.map { |part| encode_part part }.join('/')
end

#raw=(value) ⇒ Object



50
51
52
# File 'lib/ruby_tdms/path.rb', line 50

def raw=(value)
	@parts = value._?('').split(RAW_MATCHER).reject { |x| x.length == 0 }.map { |part| decode_raw_part part }
end

#to_aObject



55
56
57
# File 'lib/ruby_tdms/path.rb', line 55

def to_a
	@parts
end

#to_sObject



60
61
62
# File 'lib/ruby_tdms/path.rb', line 60

def to_s
	path
end