Решение на Четвърта задача от Николина Гюрова

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

Към профила на Николина Гюрова

Резултати

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

Код

# Ugliest thing I've ever written
module UI
class TextScreen
def self.draw(&block)
TextScreen.new.instance_eval(&block)
end
def initialize(separator: "", general_style: nil)
@separator = separator
@general_style = general_style
@result = ""
end
def label(text:, border: "", style: nil)
label = "#{border}#{text}#{border}#{@separator}"
label = @general_style ? label.send(@general_style) : label
label = style ? label.send(style) : label
@result += label
end
def vertical(border: "", style: nil, &block)
proxy = TextScreen.new separator: "\n", general_style: style
@result += proxy.instance_eval(&block)
@result = add_vertical_border(border)
@result += @separator
end
def horizontal(border: "", style: nil, &block)
proxy = TextScreen.new separator: "", general_style: style
@result += border
@result += proxy.instance_eval(&block)
@result += border
@result += @separator
end
def add_vertical_border(border)
labels = @result.split("\n")
length = (labels.max_by {|l| l.length}).length
labels.map do |label|
label.ljust(length).prepend(border).concat(border)
end.join("\n")
end
end
end

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

...F.F.F.F.

Failures:

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

Failed examples:

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

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

Николина обнови решението на 24.11.2014 13:36 (преди около 10 години)

+module UI
+ class TextScreen
+ def self.draw(&block)
+ definition_proxy = DefinitionProxy.new
+ definition_proxy.instance_eval(&block)
+ end
+ end
+
+ class DefinitionProxy
+ def label(text:)
+ text
+ end
+ end
+end

Николина обнови решението на 26.11.2014 10:43 (преди почти 10 години)

module UI
class TextScreen
def self.draw(&block)
definition_proxy = DefinitionProxy.new
definition_proxy.instance_eval(&block)
end
end
class DefinitionProxy
- def label(text:)
- text
+ def initialize(separator = "")
+ @separator = separator
+ @result = ""
+ end
+
+ def label(text:, border: "")
+ @result += border
+ @result += text
+ @result += @separator
+ @result += border
+ end
+
+ def vertical(border: "", &block)
+ definition_proxy = DefinitionProxy.new "\n"
+ @result += definition_proxy.instance_eval(&block)
+ @result = add_vertical_border(border)
+ @result += @separator
+ end
+
+ def horizontal(border: "", &block)
+ definition_proxy = DefinitionProxy.new ""
+ append_result(definition_proxy, border, &block)
+ end
+
+ def append_result(definition_proxy, border, &block)
+ @result += border
+ @result += definition_proxy.instance_eval(&block)
+ @result += border
+ @result += @separator
+ end
+
+ def add_vertical_border(border)
+ labels = @result.split("\n")
+ length = (labels.max_by {|l| l.length}).length
+ labels.map do |label|
+ label.ljust(length).prepend(border).concat(border)
+ end.join("\n")
end
end
end

Николина обнови решението на 26.11.2014 10:55 (преди почти 10 години)

module UI
class TextScreen
def self.draw(&block)
definition_proxy = DefinitionProxy.new
definition_proxy.instance_eval(&block)
end
end
class DefinitionProxy
- def initialize(separator = "")
+ def initialize(separator: "", style: nil)
@separator = separator
+ @style = style
@result = ""
end
- def label(text:, border: "")
- @result += border
- @result += text
- @result += @separator
- @result += border
+ def label(text:, border: "", style: nil)
+ label = "#{border}#{text}#{@separator}#{border}"
+ label = @style ? label.send(@style) : label
+ label = style ? label.send(style) : label
+ @result += label
end
- def vertical(border: "", &block)
- definition_proxy = DefinitionProxy.new "\n"
+ def vertical(border: "", style: nil, &block)
+ definition_proxy = DefinitionProxy.new separator: "\n", style: style
@result += definition_proxy.instance_eval(&block)
@result = add_vertical_border(border)
@result += @separator
end
- def horizontal(border: "", &block)
- definition_proxy = DefinitionProxy.new ""
- append_result(definition_proxy, border, &block)
- end
-
- def append_result(definition_proxy, border, &block)
+ def horizontal(border: "", style: nil, &block)
+ definition_proxy = DefinitionProxy.new separator: "", style: style
@result += border
@result += definition_proxy.instance_eval(&block)
@result += border
@result += @separator
end
def add_vertical_border(border)
labels = @result.split("\n")
length = (labels.max_by {|l| l.length}).length
labels.map do |label|
label.ljust(length).prepend(border).concat(border)
end.join("\n")
end
end
end

Николина обнови решението на 26.11.2014 11:24 (преди почти 10 години)

module UI
class TextScreen
def self.draw(&block)
definition_proxy = DefinitionProxy.new
definition_proxy.instance_eval(&block)
end
end
class DefinitionProxy
def initialize(separator: "", style: nil)
@separator = separator
- @style = style
+ @general_style = style
@result = ""
end
def label(text:, border: "", style: nil)
label = "#{border}#{text}#{@separator}#{border}"
- label = @style ? label.send(@style) : label
+ label = @general_style ? label.send(@general_style) : label
label = style ? label.send(style) : label
@result += label
end
def vertical(border: "", style: nil, &block)
definition_proxy = DefinitionProxy.new separator: "\n", style: style
@result += definition_proxy.instance_eval(&block)
@result = add_vertical_border(border)
@result += @separator
end
def horizontal(border: "", style: nil, &block)
definition_proxy = DefinitionProxy.new separator: "", style: style
@result += border
@result += definition_proxy.instance_eval(&block)
@result += border
@result += @separator
end
def add_vertical_border(border)
labels = @result.split("\n")
length = (labels.max_by {|l| l.length}).length
labels.map do |label|
label.ljust(length).prepend(border).concat(border)
end.join("\n")
end
end
end

Николина обнови решението на 26.11.2014 11:28 (преди почти 10 години)

+# Ugliest thing I've ever written
+
module UI
class TextScreen
def self.draw(&block)
- definition_proxy = DefinitionProxy.new
+ definition_proxy = TextScreen.new
definition_proxy.instance_eval(&block)
end
- end
- class DefinitionProxy
def initialize(separator: "", style: nil)
@separator = separator
@general_style = style
@result = ""
end
def label(text:, border: "", style: nil)
label = "#{border}#{text}#{@separator}#{border}"
label = @general_style ? label.send(@general_style) : label
label = style ? label.send(style) : label
@result += label
end
def vertical(border: "", style: nil, &block)
- definition_proxy = DefinitionProxy.new separator: "\n", style: style
+ definition_proxy = TextScreen.new separator: "\n", style: style
@result += definition_proxy.instance_eval(&block)
@result = add_vertical_border(border)
@result += @separator
end
def horizontal(border: "", style: nil, &block)
- definition_proxy = DefinitionProxy.new separator: "", style: style
+ definition_proxy = TextScreen.new separator: "", style: style
@result += border
@result += definition_proxy.instance_eval(&block)
@result += border
@result += @separator
end
def add_vertical_border(border)
labels = @result.split("\n")
length = (labels.max_by {|l| l.length}).length
labels.map do |label|
label.ljust(length).prepend(border).concat(border)
end.join("\n")
end
end
end

Николина обнови решението на 26.11.2014 11:32 (преди почти 10 години)

# Ugliest thing I've ever written
module UI
class TextScreen
def self.draw(&block)
- definition_proxy = TextScreen.new
- definition_proxy.instance_eval(&block)
+ TextScreen.new.instance_eval(&block)
end
- def initialize(separator: "", style: nil)
+ def initialize(separator: "", general_style: nil)
@separator = separator
- @general_style = style
+ @general_style = general_style
@result = ""
end
def label(text:, border: "", style: nil)
label = "#{border}#{text}#{@separator}#{border}"
label = @general_style ? label.send(@general_style) : label
label = style ? label.send(style) : label
@result += label
end
def vertical(border: "", style: nil, &block)
- definition_proxy = TextScreen.new separator: "\n", style: style
- @result += definition_proxy.instance_eval(&block)
+ proxy = TextScreen.new separator: "\n", general_style: style
+ @result += proxy.instance_eval(&block)
@result = add_vertical_border(border)
@result += @separator
end
def horizontal(border: "", style: nil, &block)
- definition_proxy = TextScreen.new separator: "", style: style
+ proxy = TextScreen.new separator: "", general_style: style
@result += border
- @result += definition_proxy.instance_eval(&block)
+ @result += proxy.instance_eval(&block)
@result += border
@result += @separator
end
def add_vertical_border(border)
labels = @result.split("\n")
length = (labels.max_by {|l| l.length}).length
labels.map do |label|
label.ljust(length).prepend(border).concat(border)
end.join("\n")
end
end
end

Николина обнови решението на 26.11.2014 11:52 (преди почти 10 години)

# Ugliest thing I've ever written
module UI
class TextScreen
def self.draw(&block)
TextScreen.new.instance_eval(&block)
end
def initialize(separator: "", general_style: nil)
@separator = separator
@general_style = general_style
@result = ""
end
def label(text:, border: "", style: nil)
- label = "#{border}#{text}#{@separator}#{border}"
+ label = "#{border}#{text}#{border}#{@separator}"
label = @general_style ? label.send(@general_style) : label
label = style ? label.send(style) : label
@result += label
end
def vertical(border: "", style: nil, &block)
proxy = TextScreen.new separator: "\n", general_style: style
@result += proxy.instance_eval(&block)
@result = add_vertical_border(border)
@result += @separator
end
def horizontal(border: "", style: nil, &block)
proxy = TextScreen.new separator: "", general_style: style
@result += border
@result += proxy.instance_eval(&block)
@result += border
@result += @separator
end
def add_vertical_border(border)
labels = @result.split("\n")
length = (labels.max_by {|l| l.length}).length
labels.map do |label|
label.ljust(length).prepend(border).concat(border)
end.join("\n")
end
end
end