Module: Kernel

Defined in:
lib/shopifydev/pry/save_json.rb

Instance Method Summary collapse

Instance Method Details

#load_json(path = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/shopifydev/pry/save_json.rb', line 17

def load_json(path=nil)
  path = UnixTree.get_path(path, require: :file)
  if path
    puts TColor.green{"loading json from #{path.to_s}..."}
    Oj.load(path)
  else
    nil
  end
end

#save_json(obj, path = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/shopifydev/pry/save_json.rb', line 2

def save_json(obj, path=nil)
  path = Pathname.new(path) unless path.nil?
  json = Oj.dump(obj, object:true, circular:true)
  path = path.sub_ext('json') unless path.nil?
  path = UnixTree.get_path(path, require: :file, new: true)
  if path
    path = path.sub_ext('json') unless path.nil?
    puts TColor.green{"writing json to #{path.to_s}..."}
    File.open(path, 'w'){|f| f.write(json)}
    true
  else
    false
  end
end