Решение на Четвърта задача от Георги Димов

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

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

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 6 успешни тест(а)
  • 5 неуспешни тест(а)

Код

module UI
class TextScreen
@@content = []
def self.draw(&block)
@@content << []
self.instance_eval(&block)
print
end
private
def self.label(args)
current_label = Label.new(args)
current_label_container = []
current_label_container << current_label
@@content.last << current_label_container
end
def self.horizontal(*args, &block)
@@content << []
self.instance_eval(&block)
last_group = @@content.last
@@content.delete(last_group)
@@content.last << horizontal_attributes(last_group, args).flatten
end
def self.horizontal_attributes(group, attributes)
return group if attributes == []
border_symbol = attributes.first[:border].to_s
group.first.last.text = border_symbol + group.first.last.text
group.last.last.text = group.last.last.text + border_symbol
group.each{|label| label.last.style = attributes.first[:style] }
end
def self.vertical(*args, &block)
self.instance_eval(&block)
@@content.last.each do |group|
group.last.text << "\n"
end
end
def self.print
result = ""
@@content.flatten.each{|label| result << label.text}
@@content = []
result
end
end
class Label
def initialize(hash)
@text = hash[:text]
@style = hash[:style]
@border = hash[:border]
end
def parse_attributes
@text = @text.send(@style) if @style != nil
@text = @border + @text + @border if @border != nil
end
def text
parse_attributes
@text
end
def text=(new_text)
@text = new_text
end
def style=(new_style)
@style = new_style if @style == nil
end
def border=(new_border)
@border = new_border if @border == nil
end
end
end

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

...FFF.F.F.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9e2b3c8@/tmp/d20141126-26053-remiah/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-remiah/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 wraps vertically-aligned components correctly in border
     Failure/Error: expect do
       expected #<Proc:0xb9e238bc@/tmp/d20141126-26053-remiah/spec.rb:85> to render as "      |something|\n      |some     |\n      |soommee  |\n"
     # /tmp/d20141126-26053-remiah/spec.rb:85: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 handles borders correctly in complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9e0d378@/tmp/d20141126-26053-remiah/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-remiah/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)>'

  4) Command Line Toolkit propagates upcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9df9c4c@/tmp/d20141126-26053-remiah/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-remiah/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)>'

  5) Command Line Toolkit propagates downcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9de45a4@/tmp/d20141126-26053-remiah/spec.rb:172> to render as "      SOMEVERYinterestingGET IT?\n              textgoes       \n                  here       \n"
     # /tmp/d20141126-26053-remiah/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.02332 seconds
11 examples, 5 failures

Failed examples:

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

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

Георги обнови решението на 26.11.2014 15:25 (преди почти 10 години)

+module UI
+ class TextScreen
+ @@content = []
+ def self.draw(&block)
+ @@content << []
+ self.instance_eval(&block)
+ print
+ end
+
+ def self.label(args)
+ current_label = Label.new(args)
+ current_label_container = []
+ current_label_container << current_label
+ @@content.last << current_label_container
+ end
+
+ def self.horizontal(*args, &block)
+ @@content << []
+ self.instance_eval(&block)
+
+ last_group = @@content.last
+ @@content.delete(last_group)
+
+ @@content.last << horizontal_attributes(last_group, args)
+ end
+
+ def self.horizontal_attributes(group, attributes)
+ return group if attributes == []
+
+ border_symbol = attributes.first[:border].to_s
+ group.first.last.text = border_symbol + group.first.last.text
+ group.last.last.text = group.last.last.text + border_symbol
+
+ group.each{|label| label.last.style = attributes.first[:style] }
+ end
+
+ def self.vertical(*args, &block)
+ @@content << []
+ self.instance_eval(&block)
+
+ @@content.last.each do |group|
+ group.last.last.text << "\n"
+ end
+ end
+
+ def self.print
+ result = ""
+ @@content.flatten.each{|label| result << label.text }
+ p @@content
+ result
+ end
+
+ end
+
+ class Label
+ def initialize(hash)
+ @text = hash[:text]
+ @style = hash[:style]
+ @border = hash[:border]
+ end
+
+ def parse_attributes
+ @text = @text.send(@style) if @style != nil
+ @text = @border + @text + @border if @border != nil
+ end
+
+ def text
+ parse_attributes
+ @text
+ end
+
+ def text=(new_text)
+ @text = new_text
+ end
+
+ def style=(new_style)
+ @style = new_style if @style == nil
+ end
+
+ def border=(new_border)
+ @border = new_border if @border == nil
+ end
+ end
+end

Георги обнови решението на 26.11.2014 16:13 (преди почти 10 години)

module UI
class TextScreen
@@content = []
def self.draw(&block)
@@content << []
self.instance_eval(&block)
print
end
+private
def self.label(args)
current_label = Label.new(args)
current_label_container = []
current_label_container << current_label
@@content.last << current_label_container
end
def self.horizontal(*args, &block)
@@content << []
self.instance_eval(&block)
last_group = @@content.last
@@content.delete(last_group)
@@content.last << horizontal_attributes(last_group, args)
end
def self.horizontal_attributes(group, attributes)
return group if attributes == []
border_symbol = attributes.first[:border].to_s
group.first.last.text = border_symbol + group.first.last.text
group.last.last.text = group.last.last.text + border_symbol
group.each{|label| label.last.style = attributes.first[:style] }
end
def self.vertical(*args, &block)
@@content << []
self.instance_eval(&block)
@@content.last.each do |group|
- group.last.last.text << "\n"
+ group.last.text << "\n"
end
end
def self.print
result = ""
@@content.flatten.each{|label| result << label.text }
- p @@content
+ @@content = []
result
end
-
end
class Label
def initialize(hash)
@text = hash[:text]
@style = hash[:style]
@border = hash[:border]
end
def parse_attributes
@text = @text.send(@style) if @style != nil
@text = @border + @text + @border if @border != nil
end
def text
parse_attributes
@text
end
def text=(new_text)
@text = new_text
end
def style=(new_style)
@style = new_style if @style == nil
end
def border=(new_border)
@border = new_border if @border == nil
end
end
end

Георги обнови решението на 26.11.2014 16:35 (преди почти 10 години)

module UI
class TextScreen
@@content = []
def self.draw(&block)
@@content << []
self.instance_eval(&block)
print
end
private
def self.label(args)
current_label = Label.new(args)
current_label_container = []
current_label_container << current_label
@@content.last << current_label_container
end
def self.horizontal(*args, &block)
@@content << []
self.instance_eval(&block)
last_group = @@content.last
@@content.delete(last_group)
- @@content.last << horizontal_attributes(last_group, args)
+ @@content.last << horizontal_attributes(last_group, args).flatten
end
def self.horizontal_attributes(group, attributes)
return group if attributes == []
border_symbol = attributes.first[:border].to_s
group.first.last.text = border_symbol + group.first.last.text
group.last.last.text = group.last.last.text + border_symbol
group.each{|label| label.last.style = attributes.first[:style] }
end
def self.vertical(*args, &block)
- @@content << []
self.instance_eval(&block)
@@content.last.each do |group|
group.last.text << "\n"
end
end
def self.print
result = ""
- @@content.flatten.each{|label| result << label.text }
+ @@content.flatten.each{|label| result << label.text}
@@content = []
result
end
end
class Label
def initialize(hash)
@text = hash[:text]
@style = hash[:style]
@border = hash[:border]
end
def parse_attributes
@text = @text.send(@style) if @style != nil
@text = @border + @text + @border if @border != nil
end
def text
parse_attributes
@text
end
def text=(new_text)
@text = new_text
end
def style=(new_style)
@style = new_style if @style == nil
end
def border=(new_border)
@border = new_border if @border == nil
end
end
end