Стилиян обнови решението на 10.11.2014 01:44 (преди около 10 години)
+module RBFS
+ def to_boolean(str)
+ str == 'true'
+ end
+
+ class RBFS::File
+ attr_accessor :data
+ attr_accessor :data_type
+
+ def File.parse(string_data)
+ type_and_data = string_data.partition(":")
+ file_type = File.get_type(type_and_data[0])
+ file_content = File.convert_data( type_and_data[2], file_type)
+ parsed_file = RBFS::File.new(file_content)
+ end
+ def File.get_type(string_data)
+ case string_data
+ when "numbers" then :numbers
+ when "string" then :string
+ when "boolean" then :boolean
+ when "symbol" then :symbol
+ when "nil" then :nil
+ end
+ end
+
+ def File.convert_data(string_data, type)
+ case type
+ when :numbers then string_data.to_f
+ when :string then string_data
+ when :boolean then to_boolean(string_data)
+ when :symbol then string_data.to_sym
+ else
+ nil
+ end
+ end
+
+ def initialize( data_to_store_in_file )
+ @data = data_to_store_in_file
+ @data_type = self.data_type
+ end
+
+ def data_type
+ case
+ when @data.class == Fixnum, @data.class == Float then :number
+ when @data.class == String then :string
+ when @data.class == Symbol then :symbol
+ when @data.class == TrueClass, @data.class == FalseClass then :boolean
+ else
+ :nil
+ end
+ end
+
+ def serialize
+ @data_type.to_s + ":" + @data.to_s
+ end
+ end
+
+ class RBFS::Directory
+ attr_reader :files
+ attr_reader :directories
+ def initialize
+ @directories = {}
+ @files = {}
+ end
+
+ def add_file(name, file)
+ files[name] = file
+ end
+
+ def add_directory(name, directory)
+ directories[name] = directory
+ end
+
+ def [](name)
+ if directories[name] != nil
+ directories[name]
+ else
+ files[name]
+ end
+ end
+ def serialize()
+ serialized_files = serialized_hash(@files) + serialized_hash(@directories)
+ end
+ def serialized_hash(hash)
+ serialized_files = @directories.size.to_s + ":"
+ hash.each do |key, value|
+ srld_elem = value.serialize
+ file_size = srld_elem.size.to_s
+ serialized_files = serialized_files + key + ":" + file_size + ":" + srld_elem
+ end
+ serialized_files
+ end
+ end
+end