Решение на Четвърта задача от Йоана Тодорова

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

Към профила на Йоана Тодорова

Резултати

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

Код

module UI
class TextScreen
def self.draw(&block)
Group.new.tap { |group| group.instance_exec(&block) }.render
end
end
class Group
def initialize(border = nil, style = nil)
@labels = []
define_singleton_method :horizontal, horizontal_implementation(style)
define_singleton_method :vertical, vertical_implementation(style)
end
def label(**args)
horizontal { label(args) }
end
def render
@labels.join ''
end
private
def horizontal_implementation(group_style)
-> (border: nil, style: group_style, &block) do
@labels << HorizontalGroup.new(border, style, &block).render
end
end
def vertical_implementation(group_style)
-> (border: nil, style: group_style, &block) do
@labels << VerticalGroup.new(border, style, &block).render
end
end
end
class HorizontalGroup < Group
def initialize(border = nil, style = nil, &block)
super
define_singleton_method :render, render_implementation(border)
define_singleton_method :label, Label.label_implementation(style)
instance_exec(&block)
end
private
def render_implementation(border)
-> { "#{border}#{@labels.join ''}#{border}" }
end
end
class VerticalGroup < Group
def initialize(border = nil, style = nil, &block)
super
define_singleton_method :render, -> { render_implementation(border) }
define_singleton_method :label, Label.label_implementation(style)
instance_exec(&block)
end
private
def render_implementation(border)
width = @labels.max_by(&:length).length
@labels.flat_map { |label| [border, label.ljust(width), border, "\n"] }
.join ''
end
end
module Label
def self.label_implementation(group_style)
-> (text:, border: nil, style: group_style) do
@labels << Label.render(text, border, style)
end
end
def self.render(text, border, style)
"#{border}#{style ? style.to_proc.call(text) : text}#{border}"
end
end
end

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

...F.F.F.F.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9fb5c0c@/tmp/d20141126-26053-h8b619/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-h8b619/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:0xb9f9aed4@/tmp/d20141126-26053-h8b619/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-h8b619/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:0xb9f8456c@/tmp/d20141126-26053-h8b619/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-h8b619/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:0xb9f5e1a0@/tmp/d20141126-26053-h8b619/spec.rb:172> to render as "      SOMEVERYinterestingGET IT?\n              textgoes       \n                  here       \n"
     # /tmp/d20141126-26053-h8b619/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.02926 seconds
11 examples, 4 failures

Failed examples:

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

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

Йоана обнови решението на 23.11.2014 21:42 (преди около 10 години)

+module UI
+ class TextScreen
+ def self.draw(&block)
+ group = Group.new
+ group.instance_exec(&block)
+ group.render
+ end
+ end
+
+ class Group
+ def initialize(border = nil, group_style = nil, &block)
+ @labels = []
+ define_singleton_method :horizontal,
+ horizontal_implementation(group_style)
+ define_singleton_method :vertical, vertical_implementation(group_style)
+ end
+
+ def label(**args)
+ horizontal { label(args) }
+ end
+
+ def render
+ @labels.join ''
+ end
+
+ protected
+
+ def horizontal_implementation(group_style)
+ -> (border: nil, style: group_style, &block) do
+ @labels << HorizontalGroup.new(border, style, &block).render
+ end
+ end
+
+ def vertical_implementation(group_style)
+ -> (border: nil, style: group_style, &block) do
+ @labels << VerticalGroup.new(border, style, &block).render
+ end
+ end
+ end
+
+ class HorizontalGroup < Group
+ def initialize(border = nil, group_style = nil, &block)
+ super
+ define_singleton_method :render, render_implementation(border)
+ define_singleton_method :label, Label.label_implementation(group_style)
+ instance_exec(&block)
+ end
+
+ protected
+
+ def render_implementation(border)
+ -> { "#{border}#{@labels.join ''}#{border}" }
+ end
+ end
+
+ class VerticalGroup < Group
+ def initialize(border = nil, group_style = nil, &block)
+ super
+ define_singleton_method :render do
+ render_implementation(border)
+ end
+ define_singleton_method :label, Label.label_implementation(group_style)
+ instance_exec(&block)
+ end
+
+ protected
+
+ def render_implementation(border)
+ width = @labels.max_by(&:length).length
+ @labels.flat_map { |label| [border, label.ljust(width), border, "\n"] }
+ .join ''
+ end
+ end
+
+ module Label
+ def self.label_implementation(group_style)
+ -> (text:, border: nil, style: group_style) do
+ @labels << Label.render(text, border, style)
+ end
+ end
+
+ def self.render(text, border, style)
+ "#{border}#{style ? style.to_proc.call(text) : text}#{border}"
+ end
+ end
+end

Йоана обнови решението на 24.11.2014 09:02 (преди около 10 години)

module UI
class TextScreen
def self.draw(&block)
- group = Group.new
- group.instance_exec(&block)
- group.render
+ Group.new.tap { |group| group.instance_exec(&block) }.render
end
end
class Group
- def initialize(border = nil, group_style = nil, &block)
+ def initialize(border = nil, style = nil)
@labels = []
- define_singleton_method :horizontal,
- horizontal_implementation(group_style)
- define_singleton_method :vertical, vertical_implementation(group_style)
+ define_singleton_method :horizontal, horizontal_implementation(style)
+ define_singleton_method :vertical, vertical_implementation(style)
end
def label(**args)
horizontal { label(args) }
end
def render
@labels.join ''
end
- protected
+ private
def horizontal_implementation(group_style)
-> (border: nil, style: group_style, &block) do
@labels << HorizontalGroup.new(border, style, &block).render
end
end
def vertical_implementation(group_style)
-> (border: nil, style: group_style, &block) do
@labels << VerticalGroup.new(border, style, &block).render
end
end
end
class HorizontalGroup < Group
- def initialize(border = nil, group_style = nil, &block)
+ def initialize(border = nil, style = nil, &block)
super
define_singleton_method :render, render_implementation(border)
- define_singleton_method :label, Label.label_implementation(group_style)
+ define_singleton_method :label, Label.label_implementation(style)
instance_exec(&block)
end
- protected
+ private
def render_implementation(border)
-> { "#{border}#{@labels.join ''}#{border}" }
end
end
class VerticalGroup < Group
- def initialize(border = nil, group_style = nil, &block)
+ def initialize(border = nil, style = nil, &block)
super
- define_singleton_method :render do
- render_implementation(border)
- end
- define_singleton_method :label, Label.label_implementation(group_style)
+ define_singleton_method :render, -> { render_implementation(border) }
+ define_singleton_method :label, Label.label_implementation(style)
instance_exec(&block)
end
- protected
+ private
def render_implementation(border)
width = @labels.max_by(&:length).length
@labels.flat_map { |label| [border, label.ljust(width), border, "\n"] }
.join ''
end
end
module Label
def self.label_implementation(group_style)
-> (text:, border: nil, style: group_style) do
@labels << Label.render(text, border, style)
end
end
def self.render(text, border, style)
"#{border}#{style ? style.to_proc.call(text) : text}#{border}"
end
end
end