Class: PathBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*segments) ⇒ PathBuilder

Returns a new instance of PathBuilder.



2
3
4
5
6
# File 'lib/path-builder.rb', line 2

def initialize(*segments)
  @path = self.class.base_path.dup
  @args = self.class.base_args.dup
  self.call(*segments)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(segment, *args) ⇒ Object

Add a static segment (string)



41
42
43
44
45
# File 'lib/path-builder.rb', line 41

def method_missing(segment, *args)
  @path << segment.to_s
  @path += args.map{ |a| a.to_sym }
  self
end

Class Method Details

.[](*args) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/path-builder.rb', line 18

def [](*args)
  if @base_args
    @base_args += args
  else
    @base_args = args
  end
  self
end

.base_argsObject



29
30
31
# File 'lib/path-builder.rb', line 29

def base_args
  @base_args ||= []
end

.base_args=(v) ⇒ Object



26
27
28
# File 'lib/path-builder.rb', line 26

def base_args=(v)
  @base_args = v
end

.base_pathObject



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

def base_path
  @base_path ||= []
end

.base_path=(path) ⇒ Object



12
13
14
# File 'lib/path-builder.rb', line 12

def base_path=(path)
  @base_path = path
end

.break_on_emptyObject



32
33
34
# File 'lib/path-builder.rb', line 32

def break_on_empty
  @break_on_empty.nil? ? superclass.respond_to?(:break_on_empty) && superclass.break_on_empty : @break_on_empty
end

.break_on_empty=(v) ⇒ Object



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

def break_on_empty=(v)
  @break_on_empty = v
end

.versionObject



9
10
11
# File 'lib/path-builder.rb', line 9

def version
  "0.1.3"
end

Instance Method Details

#+(other) ⇒ Object



69
70
71
72
73
# File 'lib/path-builder.rb', line 69

def +(other)
  @path += other.path!
  @args += other.args!
  self
end

#-(number) ⇒ Object



65
66
67
# File 'lib/path-builder.rb', line 65

def -(number)
  @path.pop number
end

#[](*args, break_on_empty: self.class.break_on_empty) ⇒ Object Also known as: to_s



53
54
55
56
57
58
59
60
61
62
# File 'lib/path-builder.rb', line 53

def [](*args, break_on_empty: self.class.break_on_empty)
  @args += args
  @path.map do |segment|
    if segment.is_a? Symbol
      @args.shift || (break_on_empty ? nil : segment)
    else
      segment
    end
  end.take_while{|i|i}.join('/') << '/'
end

#args!Object



79
80
81
# File 'lib/path-builder.rb', line 79

def args!
  @args
end

#call(*segments) ⇒ Object

Add a segment (symbol: variable, string: static)



48
49
50
51
# File 'lib/path-builder.rb', line 48

def call(*segments)
  @path += segments.flat_map{ |s| s.split('/') }
  self
end

#inspectObject



90
91
92
# File 'lib/path-builder.rb', line 90

def inspect
  "#<#{self.class.name} /#{@path.map(&:inspect).join('/')}>"
end

#path!Object



75
76
77
# File 'lib/path-builder.rb', line 75

def path!
  @path
end

#save!(break_on_empty: nil) ⇒ Object



83
84
85
86
87
88
# File 'lib/path-builder.rb', line 83

def save!(break_on_empty: nil)
  klass = Class.new self.class
  klass.base_path = @path
  klass.break_on_empty = break_on_empty unless break_on_empty.nil?
  klass
end