Class: Leto::Path

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/leto/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start:, steps: []) ⇒ Path

Returns a new instance of Path.



8
9
10
11
# File 'lib/leto/path.rb', line 8

def initialize(start:, steps: [])
  @start = start
  @steps = steps
end

Instance Attribute Details

#startObject (readonly)

Returns the value of attribute start.



6
7
8
# File 'lib/leto/path.rb', line 6

def start
  @start
end

#stepsObject (readonly)

Returns the value of attribute steps.



6
7
8
# File 'lib/leto/path.rb', line 6

def steps
  @steps
end

Instance Method Details

#+(other) ⇒ Object



22
23
24
# File 'lib/leto/path.rb', line 22

def +(other)
  self.class.new(start: start, steps: steps + other.to_a)
end

#==(other) ⇒ Object



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

def ==(other)
  other.to_a == steps
end

#each(&block) ⇒ Object



13
14
15
# File 'lib/leto/path.rb', line 13

def each(&block)
  steps.each(&block)
end

#inspectObject Also known as: to_s



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/leto/path.rb', line 36

def inspect
  start_str = start.inspect
  start_str = "#{start_str[0..38]}…#{start_str[-1]}" if start_str.size > 40
  [
    "#<#{self.class} #{start_str}",
    steps.map do |method, *args|
      args_str = args.map(&:inspect).join(', ')
      if method == :[]
        "[#{args_str}]"
      else
        ".#{method}#{"(#{args_str})" unless args_str.empty?}"
      end
    end,
    ">"
  ].join
end

#resolveObject



26
27
28
29
30
# File 'lib/leto/path.rb', line 26

def resolve
  steps.inject(start) do |obj, (method, *args)|
    obj&.send(method, *args) or break obj
  end
end

#sizeObject Also known as: count



17
18
19
# File 'lib/leto/path.rb', line 17

def size
  steps.size
end