Нели обнови решението на 10.11.2014 02:13 (преди около 10 години)
+module RBFS
+end
+
+class RBFS::File
+
+ attr_reader :data_type
+ attr_reader :data
+
+ def initialize object=nil
+ @data_type = get_type object
+ @data = object
+ end
+
+ def data=(object)
+ @data = object
+ @data_type = get_type object
+ end
+
+ def serialize
+ "#{@data_type.to_s}:#{@data.to_s}"
+ end
+
+ def self.parse(string_data)
+ fields = string_data.split(':')
+ case fields[0]
+ when "nil" then new
+ when "string" then new fields[1]
+ when "boolean" then new to_boolean(fields[1])
+ when "symbol" then new fields[1].to_sym
+ when "number" then new to_number(fields[1])
+ end
+ end
+
+ private
+ def get_type(object)
+ return :nil if object.nil?
+ return :number if object.is_a?(Numeric)
+ return :boolean if (object == true || object == false)
+ object.class.name.downcase.to_sym
+ end
+
+ def self.to_boolean(string)
+ case string
+ when 'true' then true
+ when 'false' then false
+ end
+ end
+
+ def self.to_number(string)
+ return string.to_f unless string.match('\.|e').nil?
+ string.to_i
+ end
+
+end
+
+class RBFS::Directory
+
+ 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)
+ @directories[name] = directory
+ end
+
+ def [](name)
+ @directories[name] || @files[name]
+ end
+
+ def serialize
+ "#{@files.size}:#{contents :file}#{@directories.size}:#{contents :directory}"
+ end
+
+ def self.parse(string_data)
+
+ end
+
+ private
+ def contents type
+ puts type.to_s
+ builder = ''
+ collection = {}
+ #name:size:containment
+ case type
+ when :file then collection = @files
+ when :directory then collection = @directories
+ end
+ collection.each { |key, value| my key, value, builder }
+ builder
+ end
+
+ def my key, value, builder
+ contents = value.nil? ? '0:0:' : value.serialize
+ builder.concat("#{key}:#{contents.length}:#{contents}")
+ end
+end