Class: Urls::Runner
- Inherits:
-
Boson::Runner
- Object
- Boson::Runner
- Urls::Runner
- Defined in:
- lib/urls/runner.rb
Class Method Summary collapse
-
.init ⇒ Object
hook into init since it’s after loading rc but before command execution.
Instance Method Summary collapse
- #add(url, *desc) ⇒ Object
- #edit ⇒ Object
- #list(query = nil, options = {}) ⇒ Object
- #rm(url) ⇒ Object
- #tags ⇒ Object
Class Method Details
.init ⇒ Object
hook into init since it’s after loading rc but before command execution
9 10 11 12 |
# File 'lib/urls/runner.rb', line 9 def self.init(*) Urls.setup super end |
Instance Method Details
#add(url, *desc) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/urls/runner.rb', line 16 def add(url, *desc) = desc[-1].is_a?(Hash) ? desc.pop : {} abort "urls: #{url} already exists" if Url.first(name: Url.urlify(url)) if (obj = Url.create(name: url, desc: desc.join(' '))).saved? if [:tags] Urls.add_tag(url, [:tags]) end say "Added #{url}" else abort "urls: Failed to save url - #{obj.errors.full_messages.join(', ')}" end end |
#edit ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/urls/runner.rb', line 41 def edit unless DataMapper::Repository.adapters[:default].['adapter'] == 'yaml' abort 'urls: edit only works with yaml db' end file = Urls.home + '/urls.yml' system(ENV['EDITOR'] || 'vim', file) end |
#list(query = nil, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/urls/runner.rb', line 57 def list(query=nil, ={}) if .delete(:simple) .update headers: false, fields: [:name], tab: true end urls = if [:tag] names = Urls.tag('list', query, capture: true).split("\n") Url.all name: names elsif query Url.all(desc: /#{query}/) | Url.all(name: /#{query}/) else Url.all end Hirb.enable if [:open] choices = urls.map(&:name) cmds = Urls.browser.split(/\s+/) choices.each {|u| system(*cmds, u) } elsif [:copy] choices = urls.map(&:name) Urls.copy choices else urls = urls.map {|e| e.attributes } if !( = Urls.tagged_items).empty? urls.each {|e| e[:tags] = [e[:name]] || '' } [:fields] << :tags end puts table(urls, ) end end |
#rm(url) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/urls/runner.rb', line 31 def rm(url) if u = Url.first(name: Url.urlify(url)) u.destroy say "Deleted #{url}" else abort "urls: #{url} not found" end end |
#tags ⇒ Object
90 91 92 |
# File 'lib/urls/runner.rb', line 90 def Urls.tag('tags') end |