Class: Rowdb::Sync
Instance Method Summary collapse
-
#read ⇒ Object
Load a JSON string from a file.
-
#write(data) ⇒ Object
Save a Hash to a file as a JSON string or JS.
Methods inherited from Adapter
#find_format, #initialize, #normalize_path, #unwrap, #wrap
Constructor Details
This class inherits a constructor from Rowdb::Adapter
Instance Method Details
#read ⇒ Object
Load a JSON string from a file.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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) ⇒ Object
Save a Hash to a file as a JSON string or JS.
49 50 51 52 53 54 55 56 57 58 59 |
# 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 |