Решение на Четвърта задача от Елена Орешарова

Обратно към всички решения

Към профила на Елена Орешарова

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 7 успешни тест(а)
  • 4 неуспешни тест(а)

Код

module UI
class TextScreen
def TextScreen.draw(&block)
screen = ResultScreenBuilder.new
screen.instance_eval(&block)
end
end
class ResultScreenBuilder
def initialize
@labels = ''
end
def label(text:, border: nil, style: nil)
label_result = LabelComponent.new
@labels << label_result.label(text: text, border: border, style: style)
end
def horizontal(border: nil, style: nil, &block)
result = HorizontalGroup.new(border: border, style: style)
result.instance_eval(&block)
result.build_result
end
def vertical(border: nil, style: nil, &block)
result = VerticalGroup.new(border: border, style: style)
result.instance_eval(&block)
result.build_result
end
end
class LabelComponent
def initialize
@result = ''
end
def label(text:, border: nil, style: nil)
if style
@result << "#{border}" + "#{text.send (style)}" + "#{border}"
else
@result << "#{border}" + text + "#{border}"
end
@result
end
end
class VerticalGroup
def initialize(border: nil, style: nil)
@labels = []
@result = ''
@style = style
@border = border
end
def label(text:, border: nil, style: nil)
label = LabelComponent.new
if style
@labels << label.label(text: text, border: border, style: style)
else
@labels << label.label(text: text, border: border, style: @style)
end
center_content
end
def vertical(border: nil, style: nil, &block)
result = VerticalGroup.new(border: border, style: style)
result.instance_eval(&block)
result.build_result
end
def horizontal(border: nil, style: nil, &block)
result = HorizontalGroup.new
result.instance_eval(&block)
@labels << "#{border}" + result.build_result + "#{border}"
end
def center_content
max_length = @labels.max_by { |label| label.length }.length
@labels.map! { |label| label.ljust(max_length) }
end
def build_result
@labels.each do |label|
@result << "#{@border}" << label << "#{@border}\n"
end
@result
end
end
class HorizontalGroup
def initialize(border: nil, style: nil)
@result = ''
@labels = []
@border = border
@style = style
end
def label(text:, border: nil, style: nil)
label = LabelComponent.new
if style
@labels << label.label(text: text, border: border, style: style)
else
@labels << label.label(text: text, border: border, style: @style)
end
end
def vertical(border: nil, style: nil, &block)
result = VerticalGroup.new(border: border, style: style)
result.instance_eval(&block)
@labels << result.build_result
end
def horizontal(border: nil, style: nil, &block)
@result_string << "#{border}"
inner_builder = HorizontalGroup.new
@result_string << inner_builder.instance_eval(&block)
@result_string << "#{border}"
end
def build_result
@labels.each do |label|
@result << label
end
"#{@border}" + @result + "#{@border}"
end
end
end

Лог от изпълнението

...F.F.F.F.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xba37c6d8@/tmp/d20141126-26053-1n2moql/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-1n2moql/spec.rb:56:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xba35f7a4@/tmp/d20141126-26053-1n2moql/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-1n2moql/spec.rb:99:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Command Line Toolkit propagates upcase to child components
     Failure/Error: expect do
       expected #<Proc:0xba33f9b8@/tmp/d20141126-26053-1n2moql/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-1n2moql/spec.rb:137:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Command Line Toolkit propagates downcase to child components
     Failure/Error: expect do
       expected #<Proc:0xba325fa4@/tmp/d20141126-26053-1n2moql/spec.rb:172> to render as "      SOMEVERYinterestingGET IT?\n              textgoes       \n                  here       \n"
     # /tmp/d20141126-26053-1n2moql/spec.rb:172:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.02334 seconds
11 examples, 4 failures

Failed examples:

rspec /tmp/d20141126-26053-1n2moql/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-1n2moql/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-1n2moql/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-1n2moql/spec.rb:171 # Command Line Toolkit propagates downcase to child components

История (1 версия и 0 коментара)

Елена обнови решението на 26.11.2014 09:37 (преди почти 10 години)

+module UI
+ class TextScreen
+ def TextScreen.draw(&block)
+ screen = ResultScreenBuilder.new
+ screen.instance_eval(&block)
+ end
+ end
+
+ class ResultScreenBuilder
+ def initialize
+ @labels = ''
+ end
+
+ def label(text:, border: nil, style: nil)
+ label_result = LabelComponent.new
+ @labels << label_result.label(text: text, border: border, style: style)
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ result = HorizontalGroup.new(border: border, style: style)
+ result.instance_eval(&block)
+ result.build_result
+ end
+
+ def vertical(border: nil, style: nil, &block)
+ result = VerticalGroup.new(border: border, style: style)
+ result.instance_eval(&block)
+ result.build_result
+ end
+ end
+
+ class LabelComponent
+ def initialize
+ @result = ''
+ end
+
+ def label(text:, border: nil, style: nil)
+ if style
+ @result << "#{border}" + "#{text.send (style)}" + "#{border}"
+ else
+ @result << "#{border}" + text + "#{border}"
+ end
+ @result
+ end
+ end
+
+ class VerticalGroup
+ def initialize(border: nil, style: nil)
+ @labels = []
+ @result = ''
+ @style = style
+ @border = border
+ end
+
+ def label(text:, border: nil, style: nil)
+ label = LabelComponent.new
+ if style
+ @labels << label.label(text: text, border: border, style: style)
+ else
+ @labels << label.label(text: text, border: border, style: @style)
+ end
+ center_content
+ end
+
+ def vertical(border: nil, style: nil, &block)
+ result = VerticalGroup.new(border: border, style: style)
+ result.instance_eval(&block)
+ result.build_result
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ result = HorizontalGroup.new
+ result.instance_eval(&block)
+ @labels << "#{border}" + result.build_result + "#{border}"
+ end
+
+ def center_content
+ max_length = @labels.max_by { |label| label.length }.length
+ @labels.map! { |label| label.ljust(max_length) }
+ end
+
+ def build_result
+ @labels.each do |label|
+ @result << "#{@border}" << label << "#{@border}\n"
+ end
+ @result
+ end
+ end
+ class HorizontalGroup
+ def initialize(border: nil, style: nil)
+ @result = ''
+ @labels = []
+ @border = border
+ @style = style
+ end
+
+ def label(text:, border: nil, style: nil)
+ label = LabelComponent.new
+ if style
+ @labels << label.label(text: text, border: border, style: style)
+ else
+ @labels << label.label(text: text, border: border, style: @style)
+ end
+ end
+
+ def vertical(border: nil, style: nil, &block)
+ result = VerticalGroup.new(border: border, style: style)
+ result.instance_eval(&block)
+ @labels << result.build_result
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ @result_string << "#{border}"
+ inner_builder = HorizontalGroup.new
+ @result_string << inner_builder.instance_eval(&block)
+ @result_string << "#{border}"
+ end
+
+ def build_result
+ @labels.each do |label|
+ @result << label
+ end
+ "#{@border}" + @result + "#{@border}"
+ end
+ end
+end