Решение на Трета задача от Светослав Кръстев

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

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

Резултати

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

Код

module RBFS
class File
def initialize(data = nil)
@content = data
self.define_type
end
def define_type
@type =
case
when @content.is_a?(String) then :string
when @content.is_a?(Symbol) then :symbol
when @content.is_a?(Fixnum) || @content.is_a?(Float) then :number
when @content.is_a?(TrueClass) || @content.is_a?(FalseClass) then :boolean
else :nil
end
end
def data_type
return @type
end
def data=(new_data)
@content = new_data
self.define_type
end
def serialize
@type.to_s + ':' + @content.to_s
end
def self.parse(string_data)
array = string_data.split(':', 2)
case array[0]
when 'string' then File.new(array[1])
when 'symbol' then File.new(':' + array[1])
when 'number' then File.new(to_number(array[1]))
when 'boolean' then File.new(to_boolean(array[1]))
else File.new
end
end
def self.to_boolean(string)
string == 'true'
end
def self.to_number(string)
if string.include?('.')
string.to_f
else
string.to_i
end
end
end
class BaseDirectory
attr_reader :files
attr_reader :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = nil)
if directory
@directories[name] = directory
else
@directories[name] = Directory.new
end
end
def [](name)
if @directories.has_key?(name)
@directories[name]
elsif @files.has_key?(name)
@files[name]
else
return nil
end
end
end
class Directory <BaseDirectory
def serialize
return serialize_structure(@files) + serialize_structure(@directories)
end
def serialize_structure(structure)
result = structure.size.to_s + ':'
structure.each do |name, type|
result += "#{name}:" + type.serialize.length.to_s + ':' + type.serialize
end
return result
end
def self.parse(string_data)
new_dir = Directory.new
data = string_data
data = parse_file(data, new_dir)
parse_directory(data, new_dir)
return new_dir
end
def self.parse_file(data, dir)
type_count = data.split(':', 2).first.to_i
data = data.split(':', 2).last
type_count.times do
data = data.split(':',3)
dir.add_file(data[0], RBFS::File.parse(data[2][0...data[1].to_i]))
data = data[2][(data[1].to_i)..-1]
end
return data
end
def self.parse_directory(data, dir)
type_count = data.split(':', 2).first.to_i
data = data.split(':', 2).last
type_count.times do
data = data.split(':',3)
dir.add_directory(data[0], RBFS::Directory.parse(data[2][0...data[1].to_i]))
data = data[2][(data[1].to_i)..-1]
end
return data
end
end
end

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

...........F.F.....FFF..F..FF..F..F..F..F..F

Failures:

  1) RBFS Directory serialization ::parse can parse directories with files
     Failure/Error: expect(parsed_directory['README'].data ).to eq    'Hello world!'
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8c7e97c @content="Hello world!", @type=:string>
     # /tmp/d20141111-26053-5clle0/spec.rb:111: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 recursively
     Failure/Error: expect(parsed_directory['README'].data ).to eq 'Hello world!'
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8c73af4 @content="Hello world!", @type=:string>
     # /tmp/d20141111-26053-5clle0/spec.rb:128: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 File has nil as initial data
     Failure/Error: expect(file.data).to eq nil
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8c650bc @content=nil, @type=:nil>
     # /tmp/d20141111-26053-5clle0/spec.rb:203:in `block (3 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 File can store data
     Failure/Error: expect(file.data).to eq 'hello world'
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8c64360 @content="hello world", @type=:string>
     # /tmp/d20141111-26053-5clle0/spec.rb:208:in `block (3 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 can accept data in the initializer
     Failure/Error: expect(file.data).to eq 'Hay :)'
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8c63708 @content="Hay :)", @type=:string>
     # /tmp/d20141111-26053-5clle0/spec.rb:214:in `block (3 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 nil can be parsed
     Failure/Error: expect(file.data     ).to eq nil
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8c60f44 @content=nil, @type=:nil>
     # /tmp/d20141111-26053-5clle0/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)>'

  7) RBFS File data type string can be parsed
     Failure/Error: expect(file.data     ).to eq 'Hey there'
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8d31a54 @content="Hey there", @type=:string>
     # /tmp/d20141111-26053-5clle0/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)>'

  8) RBFS File data type string can parse a string with colons
     Failure/Error: expect(file.data     ).to eq 'Hay :)'
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8d30e10 @content="Hay :)", @type=:string>
     # /tmp/d20141111-26053-5clle0/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)>'

  9) RBFS File data type symbol can be parsed
     Failure/Error: expect(file.data     ).to eq :hello
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8d2e7a0 @content=":hello", @type=:string>
     # /tmp/d20141111-26053-5clle0/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 parsed
     Failure/Error: expect(file.data     ).to eq 1234
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8d2ac90 @content=1234, @type=:number>
     # /tmp/d20141111-26053-5clle0/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)>'

  11) RBFS File data type float number can be parsed
     Failure/Error: expect(file.data     ).to eq 3.14
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8d28314 @content=3.14, @type=:number>
     # /tmp/d20141111-26053-5clle0/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)>'

  12) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data     ).to eq true
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8d256a0 @content=true, @type=:boolean>
     # /tmp/d20141111-26053-5clle0/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)>'

  13) RBFS File data type boolean false can be parsed
     Failure/Error: expect(file.data     ).to eq false
     NoMethodError:
       undefined method `data' for #<RBFS::File:0xb8d93010 @content=false, @type=:boolean>
     # /tmp/d20141111-26053-5clle0/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.05306 seconds
44 examples, 13 failures

Failed examples:

rspec /tmp/d20141111-26053-5clle0/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-5clle0/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-5clle0/spec.rb:202 # RBFS File has nil as initial data
rspec /tmp/d20141111-26053-5clle0/spec.rb:206 # RBFS File can store data
rspec /tmp/d20141111-26053-5clle0/spec.rb:211 # RBFS File can accept data in the initializer
rspec /tmp/d20141111-26053-5clle0/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-5clle0/spec.rb:247 # RBFS File data type string can be parsed
rspec /tmp/d20141111-26053-5clle0/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-5clle0/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-5clle0/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-5clle0/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-5clle0/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-5clle0/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Светослав обнови решението на 06.11.2014 00:14 (преди около 10 години)

+module RBFS
+ class File
+ def initialize(data = nil)
+ @content = data
+ self.define_type
+ end
+
+ def define_type
+ @type =
+ case
+ when @content.is_a?(String) then :string
+ when @content.is_a?(Symbol) then :symbol
+ when @content.is_a?(Fixnum) || @content.is_a?(Float) then :number
+ when @content.is_a?(TrueClass) || @content.is_a?(FalseClass) then :boolean
+ else :nil
+ end
+ end
+
+ def data_type
+ return @type
+ end
+
+ def data=(new_data)
+ @content = new_data
+ self.define_type
+ end
+
+ def serialize
+ @type.to_s + ':' + @content.to_s
+ end
+
+ def self.parse(string_data)
+ array = string_data.split(':', 2)
+ case array[0]
+ when 'string' then File.new(array[1])
+ when 'symbol' then File.new(':' + array[1])
+ when 'number' then File.new(to_number(array[1]))
+ when 'boolean' then File.new(to_boolean(array[1]))
+ else File.new
+ end
+ end
+
+ def self.to_boolean(string)
+ string == 'true'
+ end
+
+ def self.to_number(string)
+ if string.include?('.')
+ string.to_f
+ else
+ string.to_i
+ end
+ end
+ end
+
+ class BaseDirectory
+ attr_reader :files
+ attr_reader :directories
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = nil)
+ if directory
+ @directories[name] = directory
+ else
+ @directories[name] = Directory.new
+ end
+ end
+
+ def [](name)
+ if @directories.has_key?(name)
+ @directories[name]
+ elsif @files.has_key?(name)
+ @files[name]
+ else
+ return nil
+ end
+ end
+
+ end
+
+ class Directory <BaseDirectory
+ def serialize
+ return serialize_structure(@files) + serialize_structure(@directories)
+ end
+
+ def serialize_structure(structure)
+ result = structure.size.to_s + ':'
+ structure.each do |name, type|
+ result += "#{name}:" + type.serialize.length.to_s + ':' + type.serialize
+ end
+ return result
+ end
+
+ def self.parse(string_data)
+ new_dir = Directory.new
+ data = string_data
+ data = parse_file(data, new_dir)
+ parse_directory(data, new_dir)
+ return new_dir
+ end
+
+ def self.parse_file(data, dir)
+ type_count = data.split(':', 2).first.to_i
+ data = data.split(':', 2).last
+ type_count.times do
+ data = data.split(':',3)
+ dir.add_file(data[0], RBFS::File.parse(data[2][0...data[1].to_i]))
+ data = data[2][(data[1].to_i)..-1]
+ end
+
+ return data
+ end
+
+ def self.parse_directory(data, dir)
+ type_count = data.split(':', 2).first.to_i
+ data = data.split(':', 2).last
+ type_count.times do
+ data = data.split(':',3)
+ dir.add_directory(data[0], RBFS::Directory.parse(data[2][0...data[1].to_i]))
+ data = data[2][(data[1].to_i)..-1]
+ end
+
+ return data
+ end
+ end
+end