Class: Urls::Runner

Inherits:
Boson::Runner
  • Object
show all
Defined in:
lib/urls/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initObject

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)
  options = 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 options[:tags]
      Urls.add_tag(url, options[:tags])
    end
    say "Added #{url}"
  else
    abort "urls: Failed to save url - #{obj.errors.full_messages.join(', ')}"
  end
end

#editObject



41
42
43
44
45
46
47
# File 'lib/urls/runner.rb', line 41

def edit
  unless DataMapper::Repository.adapters[:default].options['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, options={})
  if options.delete(:simple)
    options.update headers: false, fields: [:name], tab: true
  end

  urls = if options[: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 options[:open]
    choices = menu urls.map(&:name)
    cmds = Urls.browser.split(/\s+/)
    choices.each {|u| system(*cmds, u) }
  elsif options[:copy]
    choices = menu urls.map(&:name)
    Urls.copy choices
  else
    urls = urls.map {|e| e.attributes }
    if !(url_tags = Urls.tagged_items).empty?
      urls.each {|e| e[:tags] = url_tags[e[:name]] || '' }
      options[:fields] << :tags
    end
    puts table(urls, options)
  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

#tagsObject



90
91
92
# File 'lib/urls/runner.rb', line 90

def tags
  Urls.tag('tags')
end