class Grubber::Store

Constants

DEFAULT_PATH

Attributes

content[RW]
path[RW]

Public Class Methods

load() click to toggle source
# File lib/grubber/store.rb, line 17
def self.load
  client = new

  data = client.read
  return data unless data.nil?

  client.reset
end
new(opts={}) click to toggle source
# File lib/grubber/store.rb, line 11
def initialize(opts={})
  @path = opts[:path] || DEFAULT_PATH
  
  setup
end
update(data) click to toggle source
# File lib/grubber/store.rb, line 26
def self.update(data)
  return unless data.is_a?(Hash)
  
  client = new
  client.content[:grubber].merge!(data)
  client.write
  data
end

Public Instance Methods

base_config() click to toggle source
# File lib/grubber/store.rb, line 69
def base_config
  lat, lng      = *Lost.current_position
  current_time  = Time.now.to_i

  { 
    grubber: {
      location: {
        lat: lat,
        lng: lng  
      },
      created_at: current_time
    }
  }
end
read() click to toggle source
# File lib/grubber/store.rb, line 41
def read
  begin
    yml = YAML.load_file(path)
  rescue TypeError => e
    return nil
  end
  
  return nil unless yml

  yml
end
reload() click to toggle source
# File lib/grubber/store.rb, line 53
def reload
  @content = read
end
reset() click to toggle source
# File lib/grubber/store.rb, line 61
def reset
  File.open(path, 'w') do |f|
    f.write(base_config.to_yaml)
  end

  reload
end
setup() click to toggle source
# File lib/grubber/store.rb, line 35
def setup
  return reload if File.exist?(path)

  reset
end
write() click to toggle source
# File lib/grubber/store.rb, line 57
def write
  File.open(path, 'w'){|f| f.write(content.to_yaml) }
end