Решение на Трета задача от Кристиан Цветков

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

Към профила на Кристиан Цветков

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def self.parse(string_data)
File.new(string_data.split(':', 2)[1])
end
def initialize(data = nil)
@data = data
end
def data_type()
case @data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when Fixnum, Float then :number
when TrueClass, FalseClass then :boolean
end
end
def serialize()
"#{data_type()}:#{data}"
end
end
class Directory
attr_reader :files, :directories
def self.parse(string_data)
#DirectoryParser.parse(string_data)
end
def initialize()
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = nil)
unless directory.nil?
@directories[name] = directory
else
@directories[name] = Directory.new
end
end
def [](name)
if @directories.has_key? name
@directories[name]
else
@files[name]
end
end
def serialize()
DirectorySerializer.serialize(self)
end
end
class FileSerializer
def self.serialize(files)
result = "#{files.count}:"
files.each do |name, file|
serialized = file.serialize
result += "#{name}:#{serialized.length}:#{serialized}"
end
result
end
end
class DirectorySerializer
def self.serialize(dir)
"#{FileSerializer.serialize(dir.files)}" +
"#{serialzie_directories(dir.directories)}"
end
private
def self.serialzie_directories(directories)
# try Enumerble#each_with_object([])
result = "#{directories.count}:"
directories.each do |name, directory|
dir = DirectorySerializer.serialize(directory)
result += "#{name}:#{dir.size}:#{dir}"
end
result
end
end
end

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

..........FFFF..........F......F..F..F..F..F

Failures:

  1) RBFS Directory serialization ::parse can parse empty directories
     Failure/Error: expect(parsed_directory.files      ).to eq({})
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:103:in `block (5 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) RBFS Directory serialization ::parse can parse directories with files
     Failure/Error: expect(parsed_directory.files.size     ).to eq    2
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:110:in `block (5 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) RBFS Directory serialization ::parse can parse directory trees without files
     Failure/Error: expect(parsed_directory['dir1']        ).to be_an RBFS::Directory
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:119:in `block (5 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) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: expect(parsed_directory.files.size     ).to eq 2
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:127:in `block (5 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) RBFS File data type nil can be parsed
     Failure/Error: expect(file.data     ).to eq nil
       
       expected: nil
            got: ""
       
       (compared using ==)
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:231:in `block (5 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) RBFS File data type symbol can be parsed
     Failure/Error: expect(file.data     ).to eq :hello
       
       expected: :hello
            got: "hello"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:hello
       +"hello"
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:276:in `block (5 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) RBFS File data type number can be parsed
     Failure/Error: expect(file.data     ).to eq 1234
       
       expected: 1234
            got: "1234"
       
       (compared using ==)
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:295:in `block (5 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) RBFS File data type float number can be parsed
     Failure/Error: expect(file.data     ).to eq 3.14
       
       expected: 3.14
            got: "3.14"
       
       (compared using ==)
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:314:in `block (5 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) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data     ).to eq true
       
       expected: true
            got: "true"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -true
       +"true"
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:334:in `block (6 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) RBFS File data type boolean false can be parsed
     Failure/Error: expect(file.data     ).to eq false
       
       expected: false
            got: "false"
       
       (compared using ==)
     # /tmp/d20141111-26053-1bs1ex4/spec.rb:353:in `block (6 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.04363 seconds
44 examples, 10 failures

Failed examples:

rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-1bs1ex4/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Кристиан обнови решението на 10.11.2014 09:04 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+
+ def self.parse(string_data)
+ File.new(string_data.split(':', 2)[1])
+ end
+
+ def initialize(data = nil)
+ @data = data
+ end
+
+ def data_type()
+ case @data
+ when NilClass then :nil
+ when String then :string
+ when Symbol then :symbol
+ when Fixnum, Float then :number
+ when TrueClass, FalseClass then :boolean
+ end
+ end
+
+ def serialize()
+ "#{data_type()}:#{data}"
+ end
+ end
+
+ class Directory
+ attr_reader :files, :directories
+
+ def self.parse(string_data)
+ #DirectoryParser.parse(string_data)
+ end
+
+ def initialize()
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = nil)
+ unless directory.nil?
+ @directories[name] = directory
+ else
+ @directories[name] = Directory.new
+ end
+ end
+
+ def [](name)
+ if @directories.has_key? name
+ @directories[name]
+ else
+ @files[name]
+ end
+ end
+
+ def serialize()
+ DirectorySerializer.serialize(self)
+ end
+ end
+
+ class FileSerializer
+ def self.serialize(files)
+ result = "#{files.count}:"
+ files.each do |name, file|
+ serialized = file.serialize
+ result += "#{name}:#{serialized.length}:#{serialized}"
+ end
+ result
+ end
+ end
+
+ class DirectorySerializer
+ def self.serialize(dir)
+ "#{FileSerializer.serialize(dir.files)}" +
+ "#{serialzie_directories(dir.directories)}"
+ end
+
+ private
+
+ def self.serialzie_directories(directories)
+ # try Enumerble#each_with_object([])
+ result = "#{directories.count}:"
+ directories.each do |name, directory|
+ dir = DirectorySerializer.serialize(directory)
+ result += "#{name}:#{dir.size}:#{dir}"
+ end
+ result
+ end
+ end
+end