Class: Flock::MockService

Inherits:
Object
  • Object
show all
Defined in:
lib/flock/mock_service.rb

Constant Summary collapse

EXEC_OPS =
{
  Edges::ExecuteOperationType::Add => :add,
  Edges::ExecuteOperationType::Remove => :remove,
  Edges::ExecuteOperationType::Archive => :archive,
  Edges::ExecuteOperationType::Negate => :negate
}
OP_COLOR_MAP =
{:add => 0, :remove => 1, :archive => 2, :negate => 3}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fixturesObject

Returns the value of attribute fixtures.



12
13
14
# File 'lib/flock/mock_service.rb', line 12

def fixtures
  @fixtures
end

#timeoutObject

Returns the value of attribute timeout.



12
13
14
# File 'lib/flock/mock_service.rb', line 12

def timeout
  @timeout
end

Instance Method Details

#clearObject



14
15
16
# File 'lib/flock/mock_service.rb', line 14

def clear
  @graphs = nil
end

#contains(source, graph, dest) ⇒ Object



90
91
92
# File 'lib/flock/mock_service.rb', line 90

def contains(source, graph, dest)
  graphs(graph).contains?(source, dest, Edges::EdgeState::Positive)
end

#count(select_operations) ⇒ Object



94
95
96
# File 'lib/flock/mock_service.rb', line 94

def count(select_operations)
  select_query(select_operations).size
end

#execute(operations) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flock/mock_service.rb', line 42

def execute(operations)
  operations = operations.operations
  operations.each do |operation|
    term = operation.term
    position = operation.position
    graph, source = term.graph_id, term.source_id
    destinations = term.destination_ids && term.destination_ids.unpack('Q*')
    dest_state = OP_COLOR_MAP[EXEC_OPS[operation.operation_type]]

    source, destinations = destinations, source unless term.is_forward
    color_node(source, graph, destinations, dest_state, position)
  end
end

#inspectObject



38
39
40
# File 'lib/flock/mock_service.rb', line 38

def inspect
  "Flock::MockService: ( #{@forward_edges.inspect} - #{@backward_edges.inspect} )"
end

#load(fixtures = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flock/mock_service.rb', line 25

def load(fixtures = nil)
  fixtures ||= self.fixtures or raise "No flock fixtures specified. either pass fixtures to load, or set Flock::MockService.fixtures."
  clear

  fixtures.each do |fixture|
    file, graph, source, dest = fixture.values_at(:file, :graph, :source, :destination)

    load_yml(file).each do |key, row|
      color_node(row[source], graph, row[dest], OP_COLOR_MAP[:add])
    end
  end
end

#load_yml(file) ⇒ Object



18
19
20
21
22
23
# File 'lib/flock/mock_service.rb', line 18

def load_yml(file)
  @data_for_fixture ||= Hash.new do |h, k|
    h[k] = YAML::load(ERB.new(File.open(k, 'r').read).result(binding)).sort
  end
  @data_for_fixture[file]
end

#select(select_operations, page) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/flock/mock_service.rb', line 56

def select(select_operations, page)
  data, next_cursor, prev_cursor = paginate(select_query(select_operations), page)

  result = Flock::Results.new
  result.ids = data.pack("Q*")
  result.next_cursor = next_cursor
  result.prev_cursor = prev_cursor
  result
end

#select2(queries) ⇒ Object



66
67
68
69
70
# File 'lib/flock/mock_service.rb', line 66

def select2(queries)
  queries.map do |query|
    select(query.operations, query.page)
  end
end

#select_edges(queries) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/flock/mock_service.rb', line 72

def select_edges(queries)
  queries.map do |query|
    edges, next_cursor, prev_cursor = paginate(simple_query(query.term), query.page)
    result = Edges::EdgeResults.new
    result.edges = if query.term.is_forward
       edges.map(&:dup)
     else
       edges.map(&:dup).map do |edge|
         edge.source_id, edge.destination_id = edge.destination_id, edge.source_id
         edge
       end
     end
    result.next_cursor = next_cursor
    result.prev_cursor = prev_cursor
    result
  end
end