Class: Gasoline::Jerrycan

Inherits:
Object
  • Object
show all
Defined in:
lib/gasoline/jerrycan.rb

Overview

Gasoline drops are stored in a Jerrycan (safety first), in this case it is disguised as a yaml file, stored in the users’ home folder.

Not directly in the home folder but in the ‘.config/gasoline` folder, since [Rob doesn’t like them there](plus.google.com/101960720994009339267/posts/R58WgWwN9jp) and he seems to know this UNIX thingie.

Constant Summary collapse

YAML_FILE =
"#{ENV['HOME']}/.config/gasoline/drops.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJerrycan

Returns a new instance of Jerrycan.



14
15
16
17
18
# File 'lib/gasoline/jerrycan.rb', line 14

def initialize
  @drops = []
  bootstrap
  add_drops(YAML.load_file(yaml_file)[:drops]) rescue nil
end

Instance Attribute Details

#dropsObject

All the ‘Drop`s in this Jerrycan.

Returns an array of ‘Drop`s



60
61
62
# File 'lib/gasoline/jerrycan.rb', line 60

def drops
  @drops
end

Class Method Details

.load_from_array(array) ⇒ Object

Mainly used to quickly load the hash I used previously back when this was just a onefile script. Those were the days.

Returns a Jerrycan



81
82
83
84
85
# File 'lib/gasoline/jerrycan.rb', line 81

def self.load_from_array(array)
  can = new
  can.add_drops(array)
  can
end

Instance Method Details

#add_drop(drop) ⇒ Object

Adds a drop of gasoline to the jerrycan

drop - A Gasoline::Drop instance

Returns the current list of drops



25
26
27
# File 'lib/gasoline/jerrycan.rb', line 25

def add_drop(drop)
  @drops << drop
end

#add_drops(array) ⇒ Object

Takes an array of hashes and fills the can with ‘Drop`s

Hash should be:

{:name => "k", :url => "url", :description => "desc"}

Returns nothing



35
36
37
38
39
# File 'lib/gasoline/jerrycan.rb', line 35

def add_drops(array)
  array.each do |h|
    add_drop Gasoline::Drop.new_from_yml(h)
  end
end

#bootstrapObject

Will create the config file (if needed)



46
47
48
49
50
51
# File 'lib/gasoline/jerrycan.rb', line 46

def bootstrap
  return if File.exist?(yaml_file)

  FileUtils.mkdir_p(File.split(yaml_file).first)
  FileUtils.cp(example_config, yaml_file)
end

#example_configObject



53
54
55
# File 'lib/gasoline/jerrycan.rb', line 53

def example_config
  File.join(File.dirname(__FILE__), "../../example_drops.yml")
end

#saveObject

Writes the can to the yaml config file.



73
74
75
# File 'lib/gasoline/jerrycan.rb', line 73

def save
  File.open(yaml_file, 'w') {|f| f.write(to_yaml) }
end

#to_yamlObject

Goes through all the drops and squeezes a hash out of them then cunningly makes a yaml out of those.

Returns a yaml (as a String)



68
69
70
# File 'lib/gasoline/jerrycan.rb', line 68

def to_yaml
  {:drops => drops.collect(&:to_hash)}.to_yaml
end

#yaml_fileObject



41
42
43
# File 'lib/gasoline/jerrycan.rb', line 41

def yaml_file
  YAML_FILE
end