Class: Rypper::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/rypper/uri.rb

Constant Summary collapse

REGEXP_COUNTER =
/\[(\d+)\-(\d+)\:(\w+)\]/
REGEXP_NAME =
/\[\:(\w+)\]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ URI

Returns a new instance of URI.



12
13
14
# File 'lib/rypper/uri.rb', line 12

def initialize(uri)
  self.uri = uri.to_s
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



9
10
11
# File 'lib/rypper/uri.rb', line 9

def counter
  @counter
end

#orderObject (readonly)

Returns the value of attribute order.



10
11
12
# File 'lib/rypper/uri.rb', line 10

def order
  @order
end

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/rypper/uri.rb', line 8

def uri
  @uri
end

Instance Method Details

#first!Object



31
32
33
34
# File 'lib/rypper/uri.rb', line 31

def first!
  self.counter.each_value(&:first!)
  self
end

#first?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rypper/uri.rb', line 36

def first?
  self.counter.values.all?(&:first?)
end

#last!Object



60
61
62
63
# File 'lib/rypper/uri.rb', line 60

def last!
  self.counter.each_value(&:last!)
  self
end

#last?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rypper/uri.rb', line 65

def last?
  self.counter.values.all?(&:last?)
end

#next!Object



50
51
52
53
54
55
56
57
58
# File 'lib/rypper/uri.rb', line 50

def next!
  self.order.reverse.each do |name|
    cntr = self.counter[name]
    if cntr.next!.state != cntr.lower
      break
    end
  end
  self
end

#parse!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rypper/uri.rb', line 16

def parse!
  @counter = {}
  @order = []
  self.uri.scan(REGEXP_COUNTER) do
    match = $~
    lower = match[1].to_i
    upper = match[2].to_i
    digits = match[1].start_with?('0') ? match[1].length : 1
    name = match[3].intern
    self.counter[name] = Counter.new(match.to_s, lower, upper, digits)
    self.order << name
  end
  self
end

#prev!Object



40
41
42
43
44
45
46
47
48
# File 'lib/rypper/uri.rb', line 40

def prev!
  self.order.reverse.each do |name|
    cntr = self.counter[name]
    if cntr.prev!.state != cntr.upper
      break
    end
  end
  self
end

#to_path(extension = nil, cntr_sep = nil, path_sep = nil, cdigits = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rypper/uri.rb', line 83

def to_path(extension=nil, cntr_sep=nil, path_sep=nil, cdigits=nil)
  cntr_sep ||= '_'
  path_sep ||= File::Separator
  p = []
  self.order.each do |name|
    cnt = self.counter[name]
    p << name.to_s
    p << cntr_sep
    p << cnt.to_s(cdigits)
    p << path_sep
  end
  p.pop
  p << extension unless extension.nil?
  p.join()
end

#to_sObject



69
70
71
72
73
74
75
76
77
# File 'lib/rypper/uri.rb', line 69

def to_s
  s = self.uri.dup
  self.counter.each do |name, counter|
    value = counter.to_s
    s.gsub!(counter.match, value)
    s.gsub!(":[#{name}]", value)
  end
  s
end

#to_uriObject



79
80
81
# File 'lib/rypper/uri.rb', line 79

def to_uri
  ::URI.parse(self.to_s)
end