Диана обнови решението на 06.11.2014 22:04 (преди около 10 години)
+module RBFS
+ class File
+ attr_accessor :data
+ def initialize(data = nil)
+ @data = data
+ end
+
+ def data_type
+ case(@data)
+ when String then :string
+ when Symbol then :symbol
+ when Numeric then :number
+ when TrueClass, FalseClass then :boolean
+ when nil then :nil
+ end
+ end
+
+ def serialize
+ data_type.to_s + ":" + @data.to_s
+ end
+
+ def parse_number(number_string)
+ if number_string.include? "."
+ File.new(deserialize.last.to_f)
+ else
+ File.new(deserialize.last.to_i)
+ end
+ end
+
+ def File.parse(string_data)
+ deserialize = string_data.split(":",2)
+ case(deserialize.first)
+ when "nil" then File.new
+ when "number" then parse_number(deserialize.last)
+ when "string" then File.new(deserialize.last)
+ when "symbol" then File.new(deserialize.last.gsub(":", "").to_sym)
+ when "boolean" then File.new(deserialize.last == "true")
+ end
+ end
+
+ end
+
+ class Directory
+ attr_reader :files, :directories
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory=Directory.new)
+ @directories[name] = directory
+ end
+
+ def [](name)
+ if @files.include?(name)
+ @files.fetch(name)
+ else
+ @directories.fetch(name, nil)
+ end
+
+ end
+
+ def serialize
+ serialized = @files.size.to_s + ":"
+ @files.each do |key,value|
+ serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
+ end
+ serialized += @directories.size.to_s + ":"
+ @directories.each do |key,value|
+ serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
+ end
+ serialized
+ end
+
+ def Directory.parse(string_data)
+ end
+
+
+ end
+end