Class: CLabs::CaseGen::RubyArrayOutput

Inherits:
Agent show all
Defined in:
lib/agents/sets.rb

Instance Attribute Summary

Attributes inherited from Agent

#data, #reference_agents

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, reference_agents, io = STDOUT) ⇒ RubyArrayOutput

Returns a new instance of RubyArrayOutput.



277
278
279
280
281
282
283
# File 'lib/agents/sets.rb', line 277

def initialize(data, reference_agents, io=STDOUT)
  @io = io
  @struct_name = "Case"
  @struct_name = data if !data.empty?
  @agents = reference_agents
  @agents.each do |agent| execute(agent) end
end

Class Method Details

.agent_idObject



273
274
275
# File 'lib/agents/sets.rb', line 273

def self.agent_id
  "casegen:ruby_array"
end

Instance Method Details

#execute(agent) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/agents/sets.rb', line 285

def execute(agent)
  struct_header = "#{@struct_name} = Struct.new("
  struct = ''
  agent.titles.each do |title|
    struct << ', ' if !struct.empty?
    struct << ":#{title.to_u.downcase}"
  end
  struct << ')'

  guts_header = 'cases = ['
  guts = ''
  agent.combinations.each do |combo|
    guts << ",\n#{' ' * guts_header.length}" if !guts.empty?
    guts << "#{@struct_name}.new#{combo.inspect.gsub(/\[/, '(').gsub(/\]/, ')')}"
  end
  @io.print(struct_header)
  @io.print(struct)
  @io.print("\n\n")
  @io.print(guts_header)
  @io.print(guts)
  @io.print("]\n")
end