Class: Command::Results::StrategyFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/command-set/results.rb

Defined Under Namespace

Classes: FormatStrategy

Instance Attribute Summary collapse

Attributes inherited from Formatter

#advice

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Formatter

#apply_advice, #default_advice, #finish, #notify, #receive_advice, #saw_begin_list, #saw_end_list, #saw_item, #start

Constructor Details

#initialize(out = nil, err = nil) ⇒ StrategyFormatter

Returns a new instance of StrategyFormatter.



668
669
670
671
672
# File 'lib/command-set/results.rb', line 668

def initialize(out = nil, err = nil)
  super(out, err)
  @strategies = self.class.strategy_set(self)
  @strategy_stack = [@strategies[:default]]
end

Instance Attribute Details

#err_toObject (readonly)

Returns the value of attribute err_to.



674
675
676
# File 'lib/command-set/results.rb', line 674

def err_to
  @err_to
end

#out_toObject (readonly)

Returns the value of attribute out_to.



674
675
676
# File 'lib/command-set/results.rb', line 674

def out_to
  @out_to
end

Class Method Details

.inherited(sub) ⇒ Object



648
649
650
651
652
653
654
655
656
657
# File 'lib/command-set/results.rb', line 648

def inherited(sub)
  self.instance_variables.each do |var|
    value = self.instance_variable_get(var)
    if value.nil?
      sub.instance_variable_set(var, nil)
    else
      sub.instance_variable_set(var, value.dup)
    end
  end
end

.strategy(name, base_klass = FormatStrategy, &def_block) ⇒ Object



644
645
646
# File 'lib/command-set/results.rb', line 644

def strategy(name, base_klass = FormatStrategy, &def_block)
  @strategies[name.to_sym] = Class.new(base_klass, &def_block)
end

.strategy_set(formatter) ⇒ Object



659
660
661
662
663
664
665
# File 'lib/command-set/results.rb', line 659

def strategy_set(formatter)
  set = {}
  @strategies.each_pair do |name, klass|
    set[name] = klass.new(name, formatter)
  end
  return set
end

Instance Method Details

#closed_begin_list(list) ⇒ Object



695
696
697
698
699
700
701
702
703
704
705
# File 'lib/command-set/results.rb', line 695

def closed_begin_list(list)
  going_to = current_strategy.name
  unless list.options[:format_advice].nil? or 
    (next_strategy = list.options[:format_advice][:type]).nil? or
    @strategies[next_strategy].nil? or
    not current_strategy.switch_to(next_strategy)
    going_to = next_strategy
  end
  push_strategy(going_to)
  current_strategy.closed_begin_list(list)
end

#closed_end_list(list) ⇒ Object



707
708
709
710
# File 'lib/command-set/results.rb', line 707

def closed_end_list(list)
  current_strategy.closed_end_list(list)
  current_strategy.finish
end

#closed_item(item) ⇒ Object



712
713
714
715
716
717
718
719
720
721
# File 'lib/command-set/results.rb', line 712

def closed_item(item)
  unless item.options[:format_advice].nil? or 
    (once = item.options[:format_advice][:type]).nil? or
    @strategies[once].nil? or
    not current_strategy.switch_to(once)
    @strategies[once].closed_item(item)
  else
    current_strategy.closed_item(item)
  end
end

#current_strategyObject



679
680
681
# File 'lib/command-set/results.rb', line 679

def current_strategy
  @strategy_stack.last
end

#pop_strategy(name) ⇒ Object



689
690
691
692
693
# File 'lib/command-set/results.rb', line 689

def pop_strategy(name)
  if current_strategy.name == name
    @strategy_stack.pop
  end
end

#push_strategy(name) ⇒ Object



683
684
685
686
687
# File 'lib/command-set/results.rb', line 683

def push_strategy(name)
  if @strategies.has_key?(name)
    @strategy_stack.push(@strategies[name])
  end
end