class Rowdb::Sync

Public Instance Methods

read() click to toggle source

Load a JSON string from a file.

@return Hash data

# File lib/adapters/sync.rb, line 15
def read()

  json = nil

  # Load JSON inside a Javascript variable.
  if @format == :js && File.exist?(@source)
    File.open(@source, 'r') do |file|
      json = file.read
      # Fix double encoding issue due to JSON string becoming Ruby string.
      json.gsub!('\\"', '"')
      unwrap(json)
    end
  # Load JSON string.
  else
    json = Oj.load_file(@source)
  end

  unless json.nil?

    # Parse JSON.
    data = Oj.load(json)
    return data.transform_keys(&:to_sym)

  end

  return nil

end
write(data) click to toggle source

Save a Hash to a file as a JSON string or JS.

@param Hash data

# File lib/adapters/sync.rb, line 49
def write(data)

  json = Oj.dump(data, mode: :compat)

  Oj.to_file(@source, json)

  if @format == :js
    wrap()
  end

end