Class: Alfred::Handler::Callback

Inherits:
Base
  • Object
show all
Defined in:
lib/alfred/handler/callback.rb

Constant Summary collapse

ENTRIES_KEY =
'feedback_entries'

Constants inherited from Base

Base::Base_Invoke_Order

Instance Attribute Summary

Attributes inherited from Base

#order, #status

Instance Method Summary collapse

Methods inherited from Base

#<=>, #action?, #feedback, #on_action, #on_help, #options, #parser, #query, #register, #status_message, #ui, #xml_builder

Constructor Details

#initialize(alfred, opts = {}) ⇒ Callback

Returns a new instance of Callback.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/alfred/handler/callback.rb', line 23

def initialize(alfred, opts = {})
  super
  @settings = {
    :handler       => 'Callback'                  ,
    :exclusive?    => true                        ,
    :backend_dir   => @core.volatile_storage_path ,
    :backend_file  => 'callback.yaml'             ,
    :handler_order => ( Base_Invoke_Order / 12 )
  }.update(opts)

  @order = @settings[:handler_order]
end

Instance Method Details

#add_entry(entry, feedback_items) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/alfred/handler/callback.rb', line 95

def add_entry(entry, feedback_items)
  entry.merge!(:timestamp => Time.now)
  key = entry[:key]
  new_entries = entries.merge(key => entry)
  backend[ENTRIES_KEY] = new_entries
  backend[key] = feedback_items
end

#backendObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/alfred/handler/callback.rb', line 116

def backend
  @backend ||= Moneta.new(:YAML,
                          :file => File.join(@settings[:backend_dir],
                                             @settings[:backend_file]))

  unless @backend.key?(ENTRIES_KEY)
    @backend[ENTRIES_KEY] = {}
  end
  @backend
end

#entriesObject



111
112
113
# File 'lib/alfred/handler/callback.rb', line 111

def entries
  backend[ENTRIES_KEY]
end

#feedback?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/alfred/handler/callback.rb', line 43

def feedback?
  options.callback
end

#on_callback(keyword, entry, feedback_items) ⇒ Object



87
88
89
90
91
92
# File 'lib/alfred/handler/callback.rb', line 87

def on_callback(keyword, entry, feedback_items)
  add_entry(entry, feedback_items)
  Alfred::Util.notify("#{keyword} --callback '#{entry[:key]}'",
                      entry[:title] || entry[:key],
                      entry)
end

#on_closeObject



82
83
84
# File 'lib/alfred/handler/callback.rb', line 82

def on_close
  backend.close
end

#on_feedbackObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/alfred/handler/callback.rb', line 47

def on_feedback
  return unless feedback?
  if entries[options.callback]
    feedback.merge! backend[options.callback]
    @status = :exclusive if @settings[:exclusive?]

  elsif entries.empty?
    # show a warn feedback item
    feedback.add_item(
      {
        :title        => 'No available callback!' ,
        :valid        => 'no'                     ,
        :autocomplete => ''                       ,
        :subtitle     => 'Please check it later. Background task may still be running.',
        :icon         => ::Alfred::Feedback.CoreServicesIcon('Unsupported') ,
      }
    )
  else
    # list available callbacks
    entries.each do |key, entry|
      feedback.add_item(
        {
          :title        => "Feedback Callback: #{key}" ,
          :subtitle     => "#{entry[:timestamp]}",
          :valid        => 'no'                        ,
          :autocomplete => "--callback '#{key}'"       ,
          :icon         => ::Alfred::Feedback.CoreServicesIcon('AliasBadgeIcon') ,
        }.merge(entry)
      )
    end
    @status = :exclusive if @settings[:exclusive?]
  end
end

#on_parserObject



37
38
39
40
41
# File 'lib/alfred/handler/callback.rb', line 37

def on_parser
  parser.on("--callback [CALLBACK]", "Alfred callback feedback") do |v|
    options.callback = v || ''
  end
end

#remove_entry(key) ⇒ Object



103
104
105
106
107
# File 'lib/alfred/handler/callback.rb', line 103

def remove_entry(key)
  new_entries = entries.delete(key)
  backend[ENTRIES_KEY] = new_entries
  backend.delete(key)
end