Class: StructureDigest::SchemaParts::AbstractPath

Inherits:
Object
  • Object
show all
Defined in:
lib/structure_digest/abstract_path.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abstract_parts) ⇒ AbstractPath

Returns a new instance of AbstractPath.



4
5
6
# File 'lib/structure_digest/abstract_path.rb', line 4

def initialize(abstract_parts)
  @parts = abstract_parts
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



37
38
39
# File 'lib/structure_digest/abstract_path.rb', line 37

def parts
  @parts
end

Class Method Details

.from_shorthand(shorthand) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/structure_digest/abstract_path.rb', line 20

def self.from_shorthand(shorthand)
  shorthand = orig_shorthand.clone
  abstract_path = AbstractPath.new
  while !shorthand.empty?
    if key = shorthand[/^\.(\w+)/, 1]
      abstract_path = abstract_path.add_part(HashDereference.new(key))
      shorthand.sub!(/^\.\w+/, '')
    elsif shorthand[/^\[\]/]
      abstract_path = abstract_path.add_part(AbstractArrayDereference.new)
      shorthand.sub!("[]", '')
    else
      raise "shorthand #{shorthand} isn't a valid path shorthand"
    end
  end
  abstract_path
end

Instance Method Details

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



38
39
40
# File 'lib/structure_digest/abstract_path.rb', line 38

def ==(other)
  self.class == other.class && @parts == other.parts
end

#accepts(path) ⇒ Object



12
13
14
# File 'lib/structure_digest/abstract_path.rb', line 12

def accepts(path)
  self == path.abstract
end

#add_part(part) ⇒ Object



16
17
18
# File 'lib/structure_digest/abstract_path.rb', line 16

def add_part(part)
  AbstractPath.new(@parts + [part])
end

#hashObject



42
# File 'lib/structure_digest/abstract_path.rb', line 42

def hash; [@parts].hash; end

#serializeObject



8
9
10
# File 'lib/structure_digest/abstract_path.rb', line 8

def serialize
  @parts.map(&:serialize).join
end