Решение на Втора задача от Иван Георгиев

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

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

Резултати

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

Код

class Task
def initialize (status, desccription, priority, tags)
@status = status
@description = desccription
@priority = priority
@tags = tags
end
attr_accessor :status
attr_accessor :desccription
attr_accessor :priority
attr_accessor :tags
end
class Criteria
attr_reader :criteria
def initialize(criteria)
@criteria = criteria
end
def Criteria.status(status)
Criteria.new(ProcHandler.status_handler status)
end
def Criteria.priority(priority)
Criteria.new(ProcHandler.priority_handler priority)
end
def Criteria.tags(tags)
Criteria.new(ProcHandler.tags_handler tags)
end
def &(other)
Criteria.new(ProcHandler.andd criteria, other)
end
def |(other)
Criteria.new(ProcHandler.orr criteria, other)
end
def !
Criteria.new(ProcHandler.negative criteria)
end
end
class ProcHandler
def self.status_handler status
proc = Proc.new {|task| task.status == status}
Proc.new {|tasks| tasks.select &proc}
end
def self.priority_handler priority
proc = Proc.new {|task| task.priority == priority}
Proc.new {|tasks| tasks.select &proc}
end
def self.tags_handler tags
proc = Proc.new {|task| (task.tags & tags).count == tags.count}
Proc.new {|tasks| tasks.select &proc}
end
def self.selection_handler proc, tasks
tasks.select proc
end
def self.andd(criteria, other)
Proc.new {|tasks| criteria.(tasks) & other.criteria.(tasks)}
end
def self.orr(criteria, other)
Proc.new {|tasks| criteria.(tasks) | other.criteria.(tasks)}
end
def self.negative(criteria)
Proc.new {|tasks| tasks - criteria.(tasks)}
end
end
class PropertyHandler
def PropertyHandler.set_priority(map)
case(map[0])
when "TODO" then map[0] = :todo
when "CURRENT" then map[0] = :current
when "DONE" then map[0] = :done
end
end
def PropertyHandler.set_status(map)
case(map[2])
when "High" then map[2] = :high
when "Normal" then map[2] = :normal
when "Low" then map[2] = :low
end
end
def PropertyHandler.set_tags(map)
map[3] = map[3] == nil ? [] : map[3].split(',').each(&:strip!)
end
def PropertyHandler.set_props map
set_priority map
set_status map
set_tags map
Task.new map[0], map[1], map[2], map[3]
end
end
class TodoList
attr_accessor :tasks
def tasks_todo
@tasks.select {|task| task.status == :todo}.count
end
def TodoList.parse (text)
#sorry, more than 80 characters :(
a = ->(item) {item.strip}
b = ->(line) {PropertyHandler.set_props line.split("|").map &a}
TodoList.new text.each_line.map &b
end
def filter (criteria)
TodoList.new (criteria.criteria.call @tasks)
end
def tasks_completed
@tasks.select {|task| task.status == :done}.count
end
def initialize (tasks)
@tasks = tasks
end
def adjoin todoList
TodoList.new todoList.tasks | @tasks
end
def completed?
@tasks_completed == tasks.count
end
def tasks_in_progress
@tasks.select {|task| task.status == :current}.count
end
end

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

FFFFFFFFFFFF...FFFFFF.

Failures:

  1) TodoList filters tasks by status
     Failure/Error: todo_list.filter(Criteria.status :done).map(&:description).should =~ [
     NoMethodError:
       undefined method `map' for #<TodoList:0xba0ec260>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:19: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) TodoList filters tasks by priority
     Failure/Error: todo_list.filter(Criteria.priority :high).map(&:description).should =~ [
     NoMethodError:
       undefined method `map' for #<TodoList:0xba0e9ee8>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:26: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) TodoList filters tasks by tag
     Failure/Error: todo_list.filter(Criteria.tags %w[food]).map(&:description).should =~ ['Eat spaghetti.']
     NoMethodError:
       undefined method `map' for #<TodoList:0xba0e7cc4>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:36: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) TodoList filters tasks by multiple tags
     Failure/Error: todo_list.filter(Criteria.tags %w[development ruby]).map(&:description).should =~ [
     NoMethodError:
       undefined method `map' for #<TodoList:0xba0e5a8c>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:40: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) TodoList filtering by multiple tags matches only tasks with all the tags
     Failure/Error: todo_list.filter(Criteria.tags %w[development FMI]).map(&:description).should =~ ['Do the 5th Ruby challenge.']
     NoMethodError:
       undefined method `map' for #<TodoList:0xba06b6b0>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:47: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)>'

  6) TodoList supports a conjuction of filters
     Failure/Error: filtered.map(&:description).should =~ ['Eat spaghetti.', 'Destroy Facebook and Google.', 'Occupy Sofia University.']
     NoMethodError:
       undefined method `map' for #<TodoList:0xba05f284>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:52: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)>'

  7) TodoList supports a disjunction of filters
     Failure/Error: filtered.map(&:description).should =~ [
     NoMethodError:
       undefined method `map' for #<TodoList:0xba05d09c>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:57: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)>'

  8) TodoList supports a negation of filters
     Failure/Error: filtered.map(&:description).should =~ [
     NoMethodError:
       undefined method `map' for #<TodoList:0xba0cee2c>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:67: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)>'

  9) TodoList supports simple filters combination
     Failure/Error: filtered.map(&:description).should =~ ['Eat spaghetti.', 'Destroy Facebook and Google.', 'Occupy Sofia University.']
     NoMethodError:
       undefined method `map' for #<TodoList:0xb9f5feb0>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:77: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)>'

  10) TodoList supports complex filters combination
     Failure/Error: filtered.map(&:description).should =~ [
     NoMethodError:
       undefined method `map' for #<TodoList:0xb9f5d110>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:86: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)>'

  11) TodoList can be adjoined with another to-do list
     Failure/Error: adjoined.count.should eq 3
     NoMethodError:
       undefined method `count' for #<TodoList:0xb9f567e8>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:101: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)>'

  12) TodoList constructs an object for each task
     Failure/Error: task = todo_list.filter(Criteria.tags ['health']).first
     NoMethodError:
       undefined method `first' for #<TodoList:0xb9f4fe98>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:110: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)>'

  13) TodoList checks if all tasks are completed
     Failure/Error: todo_list.filter(Criteria.status :done).completed?.should eq true
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:132: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)>'

  14) TodoList doesn't modify the to-do list when filtering
     Failure/Error: todo_list.should have(text_input.lines.count).items
     NoMethodError:
       undefined method `items' for #<TodoList:0xb9f3fbb0>
     # /tmp/d20131107-4393-1ufc3xs/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)>'

  15) TodoList implements Enumerable
     Failure/Error: todo_list.should respond_to :each
       expected #<TodoList:0xb9f33680 @tasks=[#<Task:0xb9f3451c @status=:todo, @description="Eat spaghetti.", @priority=:high, @tags=["food", "happiness"]>, #<Task:0xb9f342ec @status=:todo, @description="Get 8 hours of sleep.", @priority=:low, @tags=["health"]>, #<Task:0xb9f3410c @status=:current, @description="Party animal.", @priority=:normal, @tags=["socialization"]>, #<Task:0xb9f36b3c @status=:current, @description="Grok Ruby.", @priority=:high, @tags=["development", "ruby"]>, #<Task:0xb9f33e50 @status=:done, @description="Have some tea.", @priority=:normal, @tags=[]>, #<Task:0xb9f33c98 @status=:todo, @description="Destroy Facebook and Google.", @priority=:high, @tags=["save humanity", "conspiracy"]>, #<Task:0xb9f33a54 @status=:done, @description="Do the 5th Ruby challenge.", @priority=:high, @tags=["ruby course", "FMI", "development", "ruby"]>, #<Task:0xb9f338b0 @status=:todo, @description="Find missing socks.", @priority=:low, @tags=[]>, #<Task:0xb9f33694 @status=:todo, @description="Occupy Sofia University.", @priority=:high, @tags=["#ДАНСwithMe", "#occupysu", "#оставка"]>]> to respond to :each
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:141: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)>'

  16) TodoList contains tasks with the neccessary interface
     Failure/Error: task = todo_list.first
     NoMethodError:
       undefined method `first' for #<TodoList:0xb9f29b94>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:146: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)>'

  17) TodoList tasks have an array of tags
     Failure/Error: todo_list.first.tags.should be_an Array
     NoMethodError:
       undefined method `first' for #<TodoList:0xb9f23f78>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:155: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)>'

  18) TodoList preserves the order of tasks
     Failure/Error: todo_list.map(&:description).should eq [
     NoMethodError:
       undefined method `map' for #<TodoList:0xb9f1fb44>
     # /tmp/d20131107-4393-1ufc3xs/spec.rb:159: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.03967 seconds
22 examples, 18 failures

Failed examples:

rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:18 # TodoList filters tasks by status
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:25 # TodoList filters tasks by priority
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:35 # TodoList filters tasks by tag
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:39 # TodoList filters tasks by multiple tags
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:46 # TodoList filtering by multiple tags matches only tasks with all the tags
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:96 # TodoList can be adjoined with another to-do list
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:130 # TodoList checks if all tasks are completed
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:135 # TodoList doesn't modify the to-do list when filtering
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:140 # TodoList implements Enumerable
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:145 # TodoList contains tasks with the neccessary interface
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:154 # TodoList tasks have an array of tags
rspec /tmp/d20131107-4393-1ufc3xs/spec.rb:158 # TodoList preserves the order of tasks

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

Иван обнови решението на 06.11.2013 17:07 (преди над 10 години)

+class Task
+ def initialize (status, desccription, priority, tags)
+ @status = status
+ @description = desccription
+ @priority = priority
+ @tags = tags
+ end
+ attr_accessor :status
+ attr_accessor :desccription
+ attr_accessor :priority
+ attr_accessor :tags
+end
+class Criteria
+ attr_reader :criteria
+ def initialize(criteria)
+ @criteria = criteria
+ end
+ def Criteria.status(status)
+ Criteria.new(ProcHandler.status_handler status)
+ end
+ def Criteria.priority(priority)
+ Criteria.new(ProcHandler.priority_handler priority)
+ end
+ def Criteria.tags(tags)
+ Criteria.new(ProcHandler.tags_handler tags)
+ end
+ def &(other)
+ Criteria.new(ProcHandler.andd criteria, other)
+ end
+ def |(other)
+ Criteria.new(ProcHandler.orr criteria, other)
+ end
+ def !
+ Criteria.new(ProcHandler.negative criteria)
+ end
+end
+
+class ProcHandler
+ def self.status_handler status
+ proc = Proc.new {|task| task.status == status}
+ Proc.new {|tasks| tasks.select &proc}
+ end
+ def self.priority_handler priority
+ proc = Proc.new {|task| task.priority == priority}
+ Proc.new {|tasks| tasks.select &proc}
+ end
+ def self.tags_handler tags
+ proc = Proc.new {|task| (task.tags & tags).count == tags.count}
+ Proc.new {|tasks| tasks.select &proc}
+ end
+ def self.selection_handler proc, tasks
+ tasks.select proc
+ end
+ def self.andd(criteria, other)
+ Proc.new {|tasks| criteria.(tasks) & other.criteria.(tasks)}
+ end
+ def self.orr(criteria, other)
+ Proc.new {|tasks| criteria.(tasks) | other.criteria.(tasks)}
+ end
+ def self.negative(criteria)
+ Proc.new {|tasks| tasks - criteria.(tasks)}
+ end
+end
+
+class PropertyHandler
+ def PropertyHandler.set_priority(map)
+ case(map[0])
+ when "TODO" then map[0] = :todo
+ when "CURRENT" then map[0] = :current
+ when "DONE" then map[0] = :done
+ end
+ end
+ def PropertyHandler.set_status(map)
+ case(map[2])
+ when "High" then map[2] = :high
+ when "Normal" then map[2] = :normal
+ when "Low" then map[2] = :low
+ end
+ end
+ def PropertyHandler.set_tags(map)
+ map[3] = map[3] == nil ? [] : map[3].split(',').each(&:strip!)
+ end
+
+ def PropertyHandler.set_props map
+ set_priority map
+ set_status map
+ set_tags map
+ Task.new map[0], map[1], map[2], map[3]
+ end
+end
+class TodoList
+ attr_accessor :tasks
+ def tasks_todo
+ @tasks.select {|task| task.status == :todo}.count
+ end
+ def TodoList.parse (text)
+ #sorry, more than 80 characters :(
+ a = ->(item) {item.strip}
+ b = ->(line) {PropertyHandler.set_props line.split("|").map &a}
+ TodoList.new text.each_line.map &b
+ end
+ def filter (criteria)
+ TodoList.new (criteria.criteria.call @tasks)
+ end
+ def tasks_completed
+ @tasks.select {|task| task.status == :done}.count
+ end
+ def initialize (tasks)
+ @tasks = tasks
+ end
+ def adjoin todoList
+ TodoList.new todoList.tasks | @tasks
+ end
+ def completed?
+ @tasks_completed == tasks.count
+ end
+ def tasks_in_progress
+ @tasks.select {|task| task.status == :current}.count
+ end
+end