Class: Exfuz::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/exfuz/position.rb

Instance Method Summary collapse

Constructor Details

#initialize(hierarchy) ⇒ Position

Returns a new instance of Position.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/exfuz/position.rb', line 5

def initialize(hierarchy)
  @key_to_obj = {}
  @hierarchy = hierarchy
  hierarchy.each do |h|
    k = h.keys[0]
    @key_to_obj[k] = h[k]

    instance_eval "@#{k} = h[k]", __FILE__, __LINE__
    self.class.send(:attr_reader, k)
  end
end

Instance Method Details

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



54
55
56
# File 'lib/exfuz/position.rb', line 54

def ==(other)
  other.class === self && other.hash == hash
end

#bottom_nameObject



17
18
19
# File 'lib/exfuz/position.rb', line 17

def bottom_name
  @key_to_obj.keys.last
end

#hashObject



59
60
61
# File 'lib/exfuz/position.rb', line 59

def hash
  @key_to_obj.values.hash
end

#jump_infoObject



46
47
48
49
50
51
52
# File 'lib/exfuz/position.rb', line 46

def jump_info
  keys = @key_to_obj.keys
  keys.each_with_object({}) do |key, result|
    obj = send(key)
    result.merge!(obj.jump_info)
  end
end

#match?(conditions) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/exfuz/position.rb', line 35

def match?(conditions)
  conditions.each do |key, value|
    unless obj = @key_to_obj[key]
      raise 'not exist key'
    end

    return false unless obj.match?(value)
  end
  true
end

#slice(key) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/exfuz/position.rb', line 21

def slice(key)
  keys = @key_to_obj.keys
  remaining = keys[0, (keys.find_index { |k| k == key }) + 1]
  args = @hierarchy.filter do |h|
    remaining.include?(h.keys[0])
  end
  Exfuz::Position.new(args)
end

#slice_keys(key) ⇒ Object



30
31
32
33
# File 'lib/exfuz/position.rb', line 30

def slice_keys(key)
  keys = @key_to_obj.keys
  keys[0, (keys.find_index { |k| k == key }) + 1]
end