class MeowDB

Public Class Methods

new(dir: nil, name: "database") click to toggle source
# File src/meowdb.rb, line 8
def initialize(dir: nil, name: "database")
  raise MeowDBError.new("The directory path is required") if !dir
  raise MeowDBError.new("The name of the database is required") if !name
  raise MeowDBError.new("The directory path must be an string") if !dir.is_a?(String)
  raise MeowDBError.new("The name of the database must be an string") if !name.is_a?(String)
  raise MeowDBError.new("The directory must be valid") if !Pathname.new(dir).directory?
  raise MeowDBError.new("The name must have more of one character") if name.length < 1
  raise MeowDBError.new("The name must only include letters, numbers and underscores") if !/^[a-zA-Z0-9_]+$/.match(name)

  @_options = {
    "dir" => dir,
    "name" => name
  }
  @_options["file"] = Pathname.new(@_options["dir"]).join("#{@_options["name"]}.json")
  @_utils = MeowDBUtils.new(@_options["file"])

  if !File.exist?(@_options["file"])
    File.write(@_options["file"], "{}")
  end
end

Public Instance Methods

all() click to toggle source
# File src/meowdb.rb, line 29
def all()
  return MeowDBHash.new(@_utils.get_all(), "/", @_options["file"])
end
create(id, initial_value) click to toggle source
# File src/meowdb.rb, line 33
def create(id, initial_value)
  return MeowDBError.new("The ID must only include letters, numbers, underscores and dots") if !@_utils.valid_id?(id)
  return MeowDBError.new("The value must be a string, number, hash, boolean, array or a nil") if !@_utils.valid_value?(id)
  return @_utils.get(id) if @_utils.get(id)
  object = @_utils.set(id, initial_value, true)
  return object.is_a?(Hash) ? object.merge!(__id: id) : object
end
delete(id) click to toggle source
# File src/meowdb.rb, line 41
def delete(id)
  return MeowDBError.new("The ID must only include letters, numbers, underscores and dots") if !@_utils.valid_id?(id)
  tmp_data = @_utils.get(id)
  return MeowDBError.new("That element doesn't exists in the database") if !tmp_data
  @_utils.set(id, nil, false)
  return tmp_data.is_a?(Hash) ? tmp_data.merge!(__id: id) : tmp_data
end
exist?(id) click to toggle source
# File src/meowdb.rb, line 49
def exist?(id)
  return MeowDBError.new("The ID must only include letters, numbers, underscores and dots") if !@_utils.valid_id?(id)
  return true if @_utils.get(id)
  return false
end
get(id) click to toggle source
# File src/meowdb.rb, line 55
def get(id)
  return MeowDBError.new("The ID must only include letters, numbers, underscores and dots") if !@_utils.valid_id?(id)
  data = @_utils.get(id)
  return data.is_a?(Hash) ? MeowDBHash.new(data, id, @_options["file"]) : data
end
set(id, value) click to toggle source
# File src/meowdb.rb, line 61
def set(id, value)
  return MeowDBError.new("The ID must only include letters, numbers, underscores and dots") if !@_utils.valid_id?(id)
  return MeowDBError.new("The value must be a string, number, hash, boolean, array or a nil") if !@_utils.valid_value?(value)
  return @_utils.set(id, value, false)
end