Class: Focus::Config
- Inherits:
-
Object
- Object
- Focus::Config
- Defined in:
- lib/focus/config.rb
Constant Summary collapse
- BASE_PATH =
ENV["HOME"]
- DEFAULT_BLOCK_HOSTS =
%w( reddit.com youtube.com facebook.com instagram.com twitter.com linkedin.com snapchat.com tumblr.com pinterest.com tiktok.com )
Instance Method Summary collapse
- #add_block_host(blocks) ⇒ Object
- #block_hosts ⇒ Object
- #delete_block_host(blocks) ⇒ Object
-
#initialize(filepath:) ⇒ Config
constructor
A new instance of Config.
- #read ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(filepath:) ⇒ Config
Returns a new instance of Config.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/focus/config.rb', line 11 def initialize(filepath:) @filepath = File.join(BASE_PATH, filepath) dir = File.dirname(@filepath) if !File.exist?(dir) puts "Create config directory #{dir}" Dir.mkdir(dir) end if !File.exist?(@filepath) puts "Initialize default configuration #{@filepath}" write( { block_hosts: DEFAULT_BLOCK_HOSTS, }, ) end end |
Instance Method Details
#add_block_host(blocks) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/focus/config.rb', line 41 def add_block_host(blocks) puts "This operation adds \"#{blocks}\" to the list of blocked hosts.".yellow puts "This operation does not block the hosts. Execute -b to execute the blocking.".yellow blocks.split(",").each do |block| data = read data[:block_hosts] << block write(data) puts "Added host \"#{block}\"!".green end end |
#block_hosts ⇒ Object
37 38 39 |
# File 'lib/focus/config.rb', line 37 def block_hosts read[:block_hosts] end |
#delete_block_host(blocks) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/focus/config.rb', line 52 def delete_block_host(blocks) puts "This operation deletes \"#{blocks}\" to the list of blocked hosts.".yellow puts "This operation does not unblock the hosts. Execute -b to refresh the blocking or -r to reset it.".yellow blocks.split(",").each do |block| data = read data[:block_hosts].delete_if { |host| host == block } write(data) puts "Removed host \"#{block}\"!".green end end |
#read ⇒ Object
28 29 30 |
# File 'lib/focus/config.rb', line 28 def read YAML.load_file(@filepath) end |
#write(data) ⇒ Object
32 33 34 35 |
# File 'lib/focus/config.rb', line 32 def write(data) data[:block_hosts].uniq! File.write(@filepath, data.to_yaml) end |