Решение на Трета задача от Снежана Спасова

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

Към профила на Снежана Спасова

Резултати

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

Код

module RBFS
class File
attr_reader :data_type
attr_accessor :data
def initialize(data = nil)
@data = data
@data_type = get_data_type
end
def get_data_type
case @data
when Numeric then :number
when String then :string
when Symbol then :symbol
when !!@data then :boolean
else :nil
end
end
def self.to_num(string_number)
if string_number.include? '.'
then string_number.to_f
else string_number.to_i
end
end
def self.to_bool(string_boolean)
string_boolean == 'true'
end
def self.data_from_type(string_data_type, string_data)
case string_data
when "number" then self.to_num(string_data)
when "symbol" then string_data.to_sym
when "boolean" then self.to_bool(string_data_type)
when "nil" then nil
else string_data
end
end
def serialize
@data_type = get_data_type
"#{@data_type}:#{@data}"
end
def self.parse(object)
type, data = object.split(':')
self.new (self.data_from_type(data, type))
end
end
class Directory
attr_reader :files
def initialize
@hierarchy = {}
end
def files
@hierarchy.select { |key, value| value.instance_of? RBFS::File }
end
def directories
@hierarchy.select { |key, value| value.instance_of? RBFS::Directory }
end
def add_file(name, file)
@hierarchy[name] = file unless name.include? ":"
end
def add_directory(name, direcotry = RBFS::Directory.new)
@hierarchy[name] = direcotry unless name.include? ":"
end
def [](name)
@hierarchy[name]
end
def serialize
"#{self.files.size}:" + self.files.map { |name, file|
"#{name}:#{file.serialize.length}:#{file.serialize}" }.join +
"#{self.directories.size}:" + self.directories.map {|name, dir|
"#{name}:#{dir.serialize.length}:#{dir.serialize}"}.join
end
def self.parse(object)
end
end
end

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

..........FFFF...........F.FFF.FF.FF.FF..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-1d4cc8o/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-1d4cc8o/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-1d4cc8o/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-1d4cc8o/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 string can be detected
     Failure/Error: expect(file.data_type).to eq :string
       
       expected: :string
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:string
       +:nil
     # /tmp/d20141111-26053-1d4cc8o/spec.rb:240: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 string can be parsed
     Failure/Error: expect(file.data     ).to eq 'Hey there'
       
       expected: "Hey there"
            got: "string"
       
       (compared using ==)
     # /tmp/d20141111-26053-1d4cc8o/spec.rb:250: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 string can parse a string with colons
     Failure/Error: expect(file.data     ).to eq 'Hay :)'
       
       expected: "Hay :)"
            got: "string"
       
       (compared using ==)
     # /tmp/d20141111-26053-1d4cc8o/spec.rb:257: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 symbol can be detected
     Failure/Error: expect(file.data_type).to eq :symbol
       
       expected: :symbol
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:symbol
       +:nil
     # /tmp/d20141111-26053-1d4cc8o/spec.rb:266: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 symbol can be parsed
     Failure/Error: expect(file.data     ).to eq :hello
       
       expected: :hello
            got: :symbol
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:hello
       +:symbol
     # /tmp/d20141111-26053-1d4cc8o/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)>'

  10) RBFS File data type number can be detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +:nil
     # /tmp/d20141111-26053-1d4cc8o/spec.rb:285: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)>'

  11) RBFS File data type number can be parsed
     Failure/Error: expect(file.data     ).to eq 1234
       
       expected: 1234
            got: 0
       
       (compared using ==)
     # /tmp/d20141111-26053-1d4cc8o/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)>'

  12) RBFS File data type float number can be detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +:nil
     # /tmp/d20141111-26053-1d4cc8o/spec.rb:304: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)>'

  13) RBFS File data type float number can be parsed
     Failure/Error: expect(file.data     ).to eq 3.14
       
       expected: 3.14
            got: 0
       
       (compared using ==)
     # /tmp/d20141111-26053-1d4cc8o/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)>'

  14) RBFS File data type boolean true can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-1d4cc8o/spec.rb:323: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)>'

  15) RBFS File data type boolean false can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-1d4cc8o/spec.rb:342: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.04844 seconds
44 examples, 15 failures

Failed examples:

rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:239 # RBFS File data type string can be detected
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:247 # RBFS File data type string can be parsed
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:265 # RBFS File data type symbol can be detected
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:284 # RBFS File data type number can be detected
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:303 # RBFS File data type float number can be detected
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:321 # RBFS File data type boolean true can be detected
rspec /tmp/d20141111-26053-1d4cc8o/spec.rb:340 # RBFS File data type boolean false can be detected

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

Снежана обнови решението на 10.11.2014 15:05 (преди около 10 години)

+module RBFS
+ class File
+ attr_reader :data_type
+ attr_accessor :data
+
+ def initialize(data = nil)
+ @data = data
+ @data_type = get_data_type
+ end
+
+ def get_data_type
+ case @data
+ when Numeric then :number
+ when String then :string
+ when Symbol then :symbol
+ when !!@data then :boolean
+ else :nil
+ end
+ end
+
+ def self.to_num(string_number)
+ if string_number.include? '.'
+ then string_number.to_f
+ else string_number.to_i
+ end
+ end
+
+ def self.to_bool(string_boolean)
+ string_boolean == 'true'
+ end
+
+ def self.data_from_type(string_data_type, string_data)
+ case string_data
+ when "number" then self.to_num(string_data)
+ when "symbol" then string_data.to_sym
+ when "boolean" then self.to_bool(string_data_type)
+ when "nil" then nil
+ else string_data
+ end
+ end
+
+ def serialize
+ @data_type = get_data_type
+ "#{@data_type}:#{@data}"
+ end
+
+ def self.parse(object)
+ type, data = object.split(':')
+ self.new (self.data_from_type(data, type))
+ end
+ end
+
+ class Directory
+ attr_reader :files
+
+ def initialize
+ @hierarchy = {}
+ end
+
+ def files
+ @hierarchy.select { |key, value| value.instance_of? RBFS::File }
+ end
+
+ def directories
+ @hierarchy.select { |key, value| value.instance_of? RBFS::Directory }
+ end
+
+ def add_file(name, file)
+ @hierarchy[name] = file unless name.include? ":"
+ end
+
+ def add_directory(name, direcotry = RBFS::Directory.new)
+ @hierarchy[name] = direcotry unless name.include? ":"
+ end
+
+ def [](name)
+ @hierarchy[name]
+ end
+
+ def serialize
+ "#{self.files.size}:" + self.files.map { |name, file|
+ "#{name}:#{file.serialize.length}:#{file.serialize}" }.join +
+ "#{self.directories.size}:" + self.directories.map {|name, dir|
+ "#{name}:#{dir.serialize.length}:#{dir.serialize}"}.join
+ end
+
+ def self.parse(object)
+ end
+ end
+end