Class: BoomBoom

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

Constant Summary collapse

DATAFILE =
'.boomboom'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoomBoom

Returns a new instance of BoomBoom.



12
13
14
# File 'lib/boomboom.rb', line 12

def initialize
  @boom = read || {}
end

Instance Attribute Details

#boomObject (readonly)

Returns the value of attribute boom.



9
10
11
# File 'lib/boomboom.rb', line 9

def boom
  @boom
end

Instance Method Details

#copy_to_clipboard(txt) ⇒ Object



55
56
57
58
59
# File 'lib/boomboom.rb', line 55

def copy_to_clipboard(txt)
  return if txt.nil?
  Clipboard.copy txt
  puts "Boom!".colorize(:cyan) +" We just copied #{txt.dup.colorize(:yellow)} to your clipboard."
end

#create_list(list) ⇒ Object



16
17
18
19
# File 'lib/boomboom.rb', line 16

def create_list(list)
  @boom[list] = {}
  puts "Boom!".colorize(:cyan) + " Created a new list called #{list.dup.colorize(:green)}."
end

#delete_list(list) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/boomboom.rb', line 67

def delete_list(list)
  @boom.fetch list
  puts "You sure you want to delete everything in #{list.dup.colorize(:green)}? (y/n):\n"
  decision = gets
  decision.chop!
  if decision == ?y
    @boom.delete list
    puts "Boom! Deleted all your #{list.dup.colorize(:green)}."
  elsif decision == ?n
    puts "Just kidding then."
  end
rescue KeyError
  puts "The list #{list.dup.colorize(:green)} " + "doesn't exist".colorize(:red) + "."
end

#delete_list_item(list, item) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/boomboom.rb', line 82

def delete_list_item(list, item)
  @boom.fetch list
  begin
    value = @boom[list].fetch item
  rescue KeyError
    puts "The item #{item.dup.colorize(:light_white)} " + "doesn't exist".colorize(:red) + " in the list #{list.dup.colorize(:green)}."
    return
  end
  @boom[list].delete item
  puts "Boom!".colorize(:cyan) + " #{item.dup.colorize(:light_white)} in #{list.dup.colorize(:green)} is gone forever. It was #{value.dup.colorize(:yellow)}."
rescue KeyError
  puts "The list #{list.dup.colorize(:green)} " + "doesn't exist".colorize(:red) + "."
end

#get_item(item) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/boomboom.rb', line 31

def get_item(item)
  @boom.each do |k,v|
    begin
      return v.fetch item
    rescue KeyError
      next
    end
  end
  nil
end

#get_list_item(list, item) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/boomboom.rb', line 42

def get_list_item(list, item)
  the_list = @boom.fetch list
  begin
    value = the_list.fetch item
  rescue
    puts "#{item.dup.colorize(:light_white)}" + " not found in".colorize(:red) + " #{list.dup.colorize(:yellow)}"
  end
  value
rescue KeyError
  puts "Oops! list #{list.dup.colorize(:green)} was " + "not found".colorize(:red) + "... but we'll create it..."
  create_list list
end

#has_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/boomboom.rb', line 25

def has_item?(item)
  @boom.any? do |k,v|
    v.include? item
  end
end

#has_list?(list) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/boomboom.rb', line 21

def has_list?(list)
  @boom.include? list
end

#open(target) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/boomboom.rb', line 115

def open(target)
  if has_item? target
    item = get_item target
    Launchy.open item
    puts "Boom!".colorize(:cyan) + " We just opened #{item.dup.colorize(:yellow)} for you."
  elsif has_list? target
    @boom[target].each do |k,v|
      Launchy.open v
    end
    puts "Boom!".colorize(:cyan) + " We just opened all of \"#{target.dup.colorize(:green)}\" for you."
  else
    puts "#{target} " + "not found".colorize(:red) + "."
  end
end

#open_random_item_in_list(list) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/boomboom.rb', line 130

def open_random_item_in_list(list)
  if has_list? list
    the_list = @boom[list]
    rand_item = the_list.keys[rand the_list.size]
    open rand_item
  else
    puts "#{list.dup.colorize(:green)} " + "not found".colorize(:red) + "."
  end
end


102
103
104
105
106
107
# File 'lib/boomboom.rb', line 102

def print_all
  @boom.keys.each do |k|
    puts "  #{k.dup.colorize(:green)}"
    print_list k
  end
end


109
110
111
112
113
# File 'lib/boomboom.rb', line 109

def print_boom_count
  @boom.each do |k,v|
    puts "  #{k.dup.colorize(:green)}\t (#{v.dup.size})"
  end
end


96
97
98
99
100
# File 'lib/boomboom.rb', line 96

def print_list(list)
  @boom[list].each do |k,v|
    printf "    %-#30s %s\n", k.dup.colorize(:light_white)+":", v.dup.colorize(:yellow)
  end
end

#readObject



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/boomboom.rb', line 147

def read
  begin
    file_lines = File.open(ENV['HOME']+'/'+DATAFILE, 'r') do |f|
      f.readlines
    end
  rescue Errno::ENOENT
    File.new(ENV['HOME']+'/'+DATAFILE, 'w')
    file_lines = []
  end
  return nil if file_lines.empty?
  JSON.parse file_lines.first
end

#set_list_item(list, item, value) ⇒ Object



61
62
63
64
65
# File 'lib/boomboom.rb', line 61

def set_list_item(list, item, value)
  create_list list unless has_list? list
  @boom[list][item] = value
  puts "#{item.dup.colorize(:light_white)} in #{list.dup.colorize(:green)} is #{value.dup.colorize(:yellow)}. Got it."
end

#show_helpObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/boomboom.rb', line 160

def show_help
  puts "---My Boom help---"
  puts "boomboom\t\t display high-level overview"
  puts "boomboom all\t\t show all items in all lists"
  puts "boomboom help\t\t this help text"
  puts
  puts "boomboom <list>\t\t create a new list"
  puts "boomboom <list>\t\t show items for a list"
  puts "boomboom <list> delete\t\t deletes a list"
  puts
  puts "boomboom <list> <item> <value>\t\t create a new list item"
  puts "boomboom <name>\t\t copy item's value to clipboard"
  puts "boomboom <list> <name>\t\t copy item's value to clipboard"
  puts "boomboom open <name>\t\t open item's url in browser"
  puts "boomboom open <list>\t\t open all item in a list in browser"
  puts "boomboom random\t\t open a random item's url in browser"
  puts "boomboom echo <name>\t\t echo the item's value without copying"
  puts
  puts "boomboom <list> <name> delete\t\t delete an item"
  puts "boomboom <list> delete\t\t delete all items in a list"
end

#versionObject



182
183
184
# File 'lib/boomboom.rb', line 182

def version
  puts "You're running boom #{Boomboom::VERSION}. Congratulations!"
end

#writeObject



140
141
142
143
144
145
# File 'lib/boomboom.rb', line 140

def write
  data_str = @boom.to_json
  File.open(ENV['HOME']+'/'+DATAFILE, 'w') do |f|
    f << data_str
  end
end