Светослав обнови решението на 10.11.2014 10:26 (преди около 10 години)
+module RBFS
+ class File
+ attr_accessor :data
+ attr :data_type
+
+ def initialize(object = nil)
+ @data = object
+ end
+
+ def data_type
+ case @data
+ when NilClass then @data_type = :nil
+ when String then @data_type = :string
+ when Symbol then @data_type = :symbol
+ when Fixnum, Float then @data_type = :number
+ when TrueClass, FalseClass then @data_type = :boolean
+ end
+ end
+
+ def serialize
+ "#{self.data_type}:#{@data}"
+ end
+
+ def asdf
+ @data_type
+ end
+
+ def self.parse(serialized_object)
+ object_data = serialized_object.split(':')[1]
+ case serialized_object.split(':')[0]
+ when 'symbol' then File.new(object_data.to_sym)
+ when 'boolean' then File.new(object_data == 'true')
+ when 'number' then File.new(self.to_numeric(object_data))
+ else File.new(object_data)
+ end
+ end
+
+ def to_numeric(str)
+ if str.include? '.'
+ str.to_f
+ else
+ str.to_i
+ end
+ end
+ end
+
+ class Directory
+
+ def initialize
+ @files = { }
+ @directories = { }
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = Directory.new)
+ if name.include? ':'
+ raise ArgumentError, "Directory name can't contain ':'"
+ else
+ @directories[name] = directory
+ end
+ end
+
+ def [](name)
+ if @directories[name].nil?
+ @files[name]
+ else
+ @directories[name]
+ end
+ end
+
+ def serialize
+ serialized = "#{@files.count}:"
+ @files.each do |name, file|
+ serialized += "#{name}:#{file.serialize.to_s.length}:#{file.serialize}"
+ end
+ serialized += "#{@directories.count}:"
+ @directories.each do |name, directory|
+ serialized += "#{name}:#{directory.serialize.size}:" +
+ "#{directory.serialize}"
+ end
+ serialized
+ end
+
+ def self.parse(string_data)
+
+ end
+
+ def files
+ @files.dup
+ end
+
+ def directories
+ @directories.dup
+ end
+
+ end
+end