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

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

Към профила на Емилиан Станков

Резултати

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

Код

module UI
@components = []
def self.components
@components
end
def self.components=(value)
@components = value
end
class TextScreen
attr_accessor :layout
def self.draw(&components)
self.instance_eval(&components)
text = UI.components.join
UI.components = []
text
end
def self.label(contents)
border = contents[:border]
text = contents[:text]
text = contents[:text].send(contents[:style]) if contents.has_key? :style
UI.components << "#{border}#{text}#{border}"
end
def self.horizontal(settings = {}, &components)
comps = UI.components.clone
self.instance_eval(&components)
block = UI.components - comps
text = set_text_horizontal(settings, block)
UI.components = comps + [text]
end
def self.vertical(settings = {}, &components)
comps = UI.components.clone
self.instance_eval(&components)
block = UI.components - comps
block = modify_vertical(settings, block)
UI.components = comps + [block.join]
end
def self.set_text_horizontal(settings, block)
border = settings[:border] if settings.has_key? :border
text = block.join
text = block.join.send(settings[:style]) if settings.has_key? :style
text = "#{border}#{text}#{border}" if settings.has_key? :border
text
end
def self.modify_vertical(settings, block)
settings[:border] = "" unless settings.has_key? :border
settings[:style] = :clone unless settings.has_key? :style
border = settings[:border] if settings.has_key? :border
block.map! { |comp| comp.ljust(UI.longest()).prepend(border) << border }
block.map! { |comp| comp.send(settings[:style]) }
block.map! { |comp| comp << "\n" }
end
end
def self.longest
UI.components.sort_by(&:length)[-1].length
end
end

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

...F.F.F.F.

Failures:

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

Failed examples:

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

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

Емилиан обнови решението на 22.11.2014 00:26 (преди около 10 години)

+module UI
+ @components = []
+
+ def self.components
+ @components
+ end
+
+ def self.components=(value)
+ @components = value
+ end
+
+ class TextScreen
+ attr_accessor :layout
+
+ def self.draw(&components)
+ self.instance_eval(&components)
+ text = UI.components.join
+ UI.components = []
+ text
+ end
+
+ def self.label(contents)
+ border = contents[:border]
+ text = contents[:text]
+ text = contents[:text].send(contents[:style]) if contents.has_key? :style
+ UI.components << "#{border}#{text}#{border}"
+ end
+
+ def self.horizontal(settings = {}, &components)
+ comps = UI.components.clone
+ self.instance_eval(&components)
+ block = UI.components - comps
+ text = set_text_horizontal(settings, block)
+ UI.components = comps + [text]
+ end
+
+ def self.vertical(settings = {}, &components)
+ comps = UI.components.clone
+ self.instance_eval(&components)
+ block = UI.components - comps
+ block = modify_vertical(settings, block)
+ UI.components = comps + [block.join]
+ end
+
+ def self.set_text_horizontal(settings, block)
+ border = settings[:border] if settings.has_key? :border
+ text = block.join
+ text = block.join.send(settings[:style]) if settings.has_key? :style
+ text = "#{border}#{text}#{border}" if settings.has_key? :border
+ text
+ end
+
+ def self.modify_vertical(settings, block)
+ settings[:border] = "" unless settings.has_key? :border
+ settings[:style] = :clone unless settings.has_key? :style
+ border = settings[:border] if settings.has_key? :border
+ block.map! { |comp| comp.ljust(UI.longest()).prepend(border) << border }
+ block.map! { |comp| comp.send(settings[:style]) }
+ block.map! { |comp| comp << "\n" }
+ end
+ end
+
+ def self.longest
+ UI.components.sort_by(&:length)[-1].length
+ end
+end

Прегледай примерите, които сме дискутирали в темата във форума.

Странно ми е, че си държиш компонентите в UI. Това трябва да е само namespace, който да държи други класове и модули.

Можеш да помислиш върху това да разделиш кода си на повече модули/класове с ясно дефинирани задачи.

В момента ми се струва, че set_text_horizontal, modify_vertical и longest са част от DSL-а, който изпълняваме в блока подаден на TextScreen.draw, а не трябва. Този DSL трябва да е максимално чист и опростен.