Module: GitFeats::Serializer
Instance Method Summary collapse
-
#serialize(path, data) ⇒ Object
serialize a ruby object to a file in json.
-
#unserialize(path) ⇒ Object
unserialize a json file to a ruby object.
Instance Method Details
#serialize(path, data) ⇒ Object
serialize a ruby object to a file in json
path - file path data - data to be serialized
Returns nothing
15 16 17 18 19 20 21 22 |
# File 'lib/git-feats/serializer.rb', line 15 def serialize(path, data) # Make a path to the data file if one doesn't already exist mkpath_to path File.open(path, "w") do |f| f.puts data.to_json end end |
#unserialize(path) ⇒ Object
unserialize a json file to a ruby object
path - file path
Returns a ruby object or nil
29 30 31 32 33 34 35 36 37 |
# File 'lib/git-feats/serializer.rb', line 29 def unserialize(path) if File.exists?(path) && !File.zero?(path) begin return JSON.parse(IO.binread(path)) rescue JSON::ParserError => e puts e end end end |