Class: Solve::Solver::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/solve/solver/serializer.rb

Instance Method Summary collapse

Instance Method Details

#deserialize(solver) ⇒ Solve::Solver

Parameters:

  • solver (Hash, #to_s)

    a json string or a hash representing a solver

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/solve/solver/serializer.rb', line 23

def deserialize(solver)
  unless solver.is_a?(Hash)
    solver = JSON.parse(solver.to_s)
  end

  graph_spec = solver["graph"]
  demands_spec = solver["demands"]

  graph = load_graph(graph_spec)
  demands = load_demands(demands_spec)

  Solve::Solver.new(graph, demands)
end

#serialize(solver) ⇒ String

Parameters:

Returns:

  • (String)


9
10
11
12
13
14
15
16
17
18
# File 'lib/solve/solver/serializer.rb', line 9

def serialize(solver)
  graph = solver.graph
  demands = solver.demands

  graph_hash = format_graph(graph)
  demands_hash = format_demands(demands)

  problem = graph_hash.merge(demands_hash)
  problem.to_json
end