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

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

Към профила на Ангел Ангелов

Резултати

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

Код

module UI
class Layout
def self.stylize(text, style)
case style
when :downcase then text.downcase
when :upcase then text.upcase
else text
end
end
def self.vertical_border(text, border, length)
if border
border.to_s + text.ljust(length) + border.to_s
else
text
end
end
end
class TextScreen
attr_reader :data
def initialize(style = nil)
@data = []
@style = style
end
def label(text:, border: nil, style: nil)
style = @style unless style
@data << border.to_s + Layout.stylize(text, style) + border.to_s
end
def horizontal(border: nil, style: nil, &block)
style = @style unless style
screen = self.class.new(style)
screen.instance_eval(&block)
@data << border.to_s + screen.data.join('') + border.to_s
end
def vertical(border: nil, style: nil, &block)
style = @style unless style
screen = self.class.new(style)
screen.instance_eval(&block)
@data << screen.data.map do |text|
Layout.vertical_border(text, border, screen.data.map(&:length).max)
end.join("\n")
end
def self.draw(&block)
screen = self.new
screen.instance_eval(&block)
screen.data.join('')
end
end
end

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

...F.F.F.F.

Failures:

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

Failed examples:

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

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

Ангел обнови решението на 25.11.2014 02:24 (преди около 10 години)

+module UI
+ class Layout
+ def self.stylize(text, style)
+ case style
+ when :downcase then text.downcase
+ when :upcase then text.upcase
+ else text
+ end
+ end
+
+ def self.vertical_border(text, border, length)
+ if border
+ border.to_s + text.ljust(length) + border.to_s
+ else
+ text
+ end
+ end
+ end
+
+ class TextScreen
+ attr_reader :data
+
+ def initialize
+ @data = []
+ end
+
+ def label(text:, border: nil, style: nil)
+ @data << border.to_s + Layout.stylize(text, style) + border.to_s
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ screen = self.class.new
+ screen.instance_eval(&block)
+ @data << border.to_s + screen.data.join('') + border.to_s
+ end
+
+ def vertical(border: nil, style: nil, &block)
+ screen = self.class.new
+ screen.instance_eval(&block)
+ length = screen.data.map(&:length).max
+ @data << screen.data.map do |text|
+ Layout.vertical_border(text, border, length)
+ end.join("\n")
+ end
+
+ def self.draw(&block)
+ screen = self.new
+ screen.instance_eval(&block)
+ screen.data.join('')
+ end
+ end
+end

Ангел обнови решението на 25.11.2014 02:30 (преди около 10 години)

module UI
class Layout
def self.stylize(text, style)
case style
when :downcase then text.downcase
when :upcase then text.upcase
else text
end
end
def self.vertical_border(text, border, length)
if border
border.to_s + text.ljust(length) + border.to_s
else
text
end
end
end
class TextScreen
attr_reader :data
def initialize
@data = []
end
def label(text:, border: nil, style: nil)
@data << border.to_s + Layout.stylize(text, style) + border.to_s
end
def horizontal(border: nil, style: nil, &block)
screen = self.class.new
screen.instance_eval(&block)
@data << border.to_s + screen.data.join('') + border.to_s
end
def vertical(border: nil, style: nil, &block)
screen = self.class.new
screen.instance_eval(&block)
- length = screen.data.map(&:length).max
@data << screen.data.map do |text|
- Layout.vertical_border(text, border, length)
+ Layout.vertical_border(text, border, screen.data.map(&:length).max)
end.join("\n")
end
def self.draw(&block)
screen = self.new
screen.instance_eval(&block)
screen.data.join('')
end
end
end

Ангел обнови решението на 25.11.2014 02:58 (преди около 10 години)

module UI
class Layout
def self.stylize(text, style)
case style
when :downcase then text.downcase
when :upcase then text.upcase
else text
end
end
def self.vertical_border(text, border, length)
if border
border.to_s + text.ljust(length) + border.to_s
else
text
end
end
end
class TextScreen
attr_reader :data
- def initialize
+ def initialize(style = nil)
@data = []
+ @style = style
end
def label(text:, border: nil, style: nil)
+ style = @style unless style
@data << border.to_s + Layout.stylize(text, style) + border.to_s
end
def horizontal(border: nil, style: nil, &block)
- screen = self.class.new
+ style = @style unless style
+ screen = self.class.new(style)
screen.instance_eval(&block)
@data << border.to_s + screen.data.join('') + border.to_s
end
def vertical(border: nil, style: nil, &block)
- screen = self.class.new
+ style = @style unless style
+ screen = self.class.new(style)
screen.instance_eval(&block)
@data << screen.data.map do |text|
Layout.vertical_border(text, border, screen.data.map(&:length).max)
end.join("\n")
end
def self.draw(&block)
screen = self.new
screen.instance_eval(&block)
screen.data.join('')
end
end
end