Class: AMA::Entity::Mapper::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/ama-entity-mapper/path.rb,
lib/ama-entity-mapper/path/segment.rb

Overview

Wrapper for simple array. Helps to understand where exactly processing is taking place.

Defined Under Namespace

Classes: Segment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack = []) ⇒ Path

Returns a new instance of Path.



13
14
15
# File 'lib/ama-entity-mapper/path.rb', line 13

def initialize(stack = [])
  @segments = stack
end

Instance Attribute Details

#segmentsObject (readonly)

Returns the value of attribute segments.



11
12
13
# File 'lib/ama-entity-mapper/path.rb', line 11

def segments
  @segments
end

Instance Method Details

#attribute(name) ⇒ AMA::Entity::Mapper::Path

Parameters:

  • name (String, Symbol)

Returns:



29
30
31
# File 'lib/ama-entity-mapper/path.rb', line 29

def attribute(name)
  push(Segment.attribute(name))
end

#currentAMA::Entity::Mapper::Path::Segment



49
50
51
# File 'lib/ama-entity-mapper/path.rb', line 49

def current
  @segments.last
end

#eachObject



53
54
55
56
57
# File 'lib/ama-entity-mapper/path.rb', line 53

def each
  @segments.each do |item|
    yield(item)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ama-entity-mapper/path.rb', line 17

def empty?
  @segments.empty?
end

#index(name) ⇒ AMA::Entity::Mapper::Path

Parameters:

  • name (String, Symbol, Integer)

Returns:



23
24
25
# File 'lib/ama-entity-mapper/path.rb', line 23

def index(name)
  push(Segment.index(name))
end

#merge(path) ⇒ AMA::Entity::Mapper::Path



67
68
69
# File 'lib/ama-entity-mapper/path.rb', line 67

def merge(path)
  push(*path.segments)
end

#popAMA::Entity::Mapper::Path



44
45
46
# File 'lib/ama-entity-mapper/path.rb', line 44

def pop
  self.class.new(@segments[0..-2])
end

#push(*segments) ⇒ AMA::Entity::Mapper::Path

Parameters:

Returns:



35
36
37
38
39
40
41
# File 'lib/ama-entity-mapper/path.rb', line 35

def push(*segments)
  segments = segments.map do |segment|
    next segment if segment.is_a?(Segment)
    Segment.attribute(segment)
  end
  self.class.new(@segments + segments)
end

#reduce(carrier) ⇒ Object



59
60
61
62
63
# File 'lib/ama-entity-mapper/path.rb', line 59

def reduce(carrier)
  @segments.reduce(carrier) do |inner_carrier, item|
    yield(inner_carrier, item)
  end
end

#sizeObject



71
72
73
# File 'lib/ama-entity-mapper/path.rb', line 71

def size
  @segments.size
end

#to_aArray<AMA::Entity::Mapper::Path::Segment>



80
81
82
# File 'lib/ama-entity-mapper/path.rb', line 80

def to_a
  @segments.clone
end

#to_sString

Returns:

  • (String)


85
86
87
# File 'lib/ama-entity-mapper/path.rb', line 85

def to_s
  "$#{@segments.join}"
end