Class: Command::Results::StrategyFormatter

Inherits:
TextFormatter show all
Defined in:
lib/command-set/formatter/strategy.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.



92
93
94
95
96
# File 'lib/command-set/formatter/strategy.rb', line 92

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.



98
99
100
# File 'lib/command-set/formatter/strategy.rb', line 98

def err_to
  @err_to
end

#out_toObject (readonly)

Returns the value of attribute out_to.



98
99
100
# File 'lib/command-set/formatter/strategy.rb', line 98

def out_to
  @out_to
end

Class Method Details

.inherited(sub) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/command-set/formatter/strategy.rb', line 72

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



68
69
70
# File 'lib/command-set/formatter/strategy.rb', line 68

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

.strategy_set(formatter) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/command-set/formatter/strategy.rb', line 83

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



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/command-set/formatter/strategy.rb', line 119

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



131
132
133
134
# File 'lib/command-set/formatter/strategy.rb', line 131

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

#closed_item(item) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/command-set/formatter/strategy.rb', line 136

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



103
104
105
# File 'lib/command-set/formatter/strategy.rb', line 103

def current_strategy
  @strategy_stack.last
end

#pop_strategy(name) ⇒ Object



113
114
115
116
117
# File 'lib/command-set/formatter/strategy.rb', line 113

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

#push_strategy(name) ⇒ Object



107
108
109
110
111
# File 'lib/command-set/formatter/strategy.rb', line 107

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