Class: Alfred::Feedback

Inherits:
Object
  • Object
show all
Defined in:
lib/alfred/feedback.rb,
lib/alfred/feedback/item.rb,
lib/alfred/feedback/file_item.rb,
lib/alfred/feedback/webloc_item.rb

Defined Under Namespace

Classes: FileItem, Item, WeblocItem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alfred, opts = {}, &blk) ⇒ Feedback

Returns a new instance of Feedback.



12
13
14
15
16
17
18
# File 'lib/alfred/feedback.rb', line 12

def initialize(alfred, opts = {}, &blk)
  @items = []
  @core = alfred
  use_backend(opts)
  instance_eval(&blk) if block_given?

end

Instance Attribute Details

#backend_fileObject (readonly)

Returns the value of attribute backend_file.



10
11
12
# File 'lib/alfred/feedback.rb', line 10

def backend_file
  @backend_file
end

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

Class Method Details

.CoreServicesIcon(name) ⇒ Object

## helper class method for icon



79
80
81
82
83
84
# File 'lib/alfred/feedback.rb', line 79

def self.CoreServicesIcon(name)
  {
    :type => "default" ,
    :name => "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/#{name}.icns"
  }
end

.FileIcon(path) ⇒ Object



92
93
94
95
96
97
# File 'lib/alfred/feedback.rb', line 92

def self.FileIcon(path)
  {
    :type => "fileicon" ,
    :name => path       ,
  }
end

.Icon(name) ⇒ Object



86
87
88
89
90
91
# File 'lib/alfred/feedback.rb', line 86

def self.Icon(name)
  {
    :type => "default" ,
    :name => name       ,
  }
end

Instance Method Details

#add_file_item(path, opts = {}) ⇒ Object



25
26
27
# File 'lib/alfred/feedback.rb', line 25

def add_file_item(path, opts = {})
  @items << FileItem.new(path, opts)
end

#add_item(opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
# File 'lib/alfred/feedback.rb', line 20

def add_item(opts = {})
  raise ArgumentError, "Feedback item must have title!" if opts[:title].nil?
  @items << Item.new(opts[:title], opts)
end

#add_webloc_item(path, opts = {}) ⇒ Object



29
30
31
32
33
34
# File 'lib/alfred/feedback.rb', line 29

def add_webloc_item(path, opts = {})
  unless opts[:folder]
    opts[:folder] = @core.storage_path
  end
  @items << WeblocItem.new(path, opts)
end

#append(from_file) ⇒ Object



139
140
141
# File 'lib/alfred/feedback.rb', line 139

def append(from_file)
  @items << File.open(from_file, "rb") { |f| Marshal.load(f) }
end

#closeObject

The workflow is about to complete

  • save cached feedback if necessary



72
73
74
# File 'lib/alfred/feedback.rb', line 72

def close
  put_cached_feedback if @backend_file
end

#dump(to_file) ⇒ Object



131
132
133
# File 'lib/alfred/feedback.rb', line 131

def dump(to_file)
  File.open(to_file, "wb") { |f| Marshal.dump(@items, f) }
end

#encode_with(coder) ⇒ Object



151
152
153
# File 'lib/alfred/feedback.rb', line 151

def encode_with(coder)
  coder['items'] = @items
end

#expired?Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/alfred/feedback.rb', line 114

def expired?
  return false unless @should_expire_after_second
  Time.now - File.ctime(backend_file) > @should_expire_after_second
end

#get_cached_feedbackObject



119
120
121
122
123
124
125
# File 'lib/alfred/feedback.rb', line 119

def get_cached_feedback
  return nil unless File.exist?(backend_file)
  return nil if expired?

  load(@backend_file)
  self
end

#load(from_file) ⇒ Object



135
136
137
# File 'lib/alfred/feedback.rb', line 135

def load(from_file)
  @items = File.open(from_file, "rb") { |f| Marshal.load(f) }
end

#marshal_dumpObject

Provides marshalling support for use by the Marshal library.



159
160
161
# File 'lib/alfred/feedback.rb', line 159

def marshal_dump
  @items
end

#marshal_load(x) ⇒ Object

Provides marshalling support for use by the Marshal library.



166
167
168
# File 'lib/alfred/feedback.rb', line 166

def marshal_load(x)
  @items = x
end

#merge!(other) ⇒ Object

Merge with other feedback



57
58
59
60
61
62
63
64
65
# File 'lib/alfred/feedback.rb', line 57

def merge!(other)
  if other.is_a? Array
    @items |= other
  elsif other.is_a? Alfred::Feedback
    @items |= other.items
  else
    raise ArgumentError, "Feedback can not merge with #{other.class}"
  end
end

#put_cached_feedbackObject



127
128
129
# File 'lib/alfred/feedback.rb', line 127

def put_cached_feedback
  dump(backend_file)
end

#to_xml(with_query = '', items = @items) ⇒ Object Also known as: to_alfred



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/alfred/feedback.rb', line 36

def to_xml(with_query = '', items = @items)
  document = REXML::Element.new("items")
  @items.sort!

  if with_query.empty?
    items.each do |item|
      document << item.to_xml
    end
  else
    items.each do |item|
      document << item.to_xml if item.match?(with_query)
    end
  end
  document.to_s
end

#to_yaml_propertiesObject



147
148
149
# File 'lib/alfred/feedback.rb', line 147

def to_yaml_properties
  [ '@items' ]
end

#use_backend(opts = {}) ⇒ Object Also known as: use_cache_file

## serialization



104
105
106
107
# File 'lib/alfred/feedback.rb', line 104

def use_backend(opts = {})
  @backend_file = opts[:file] if opts[:file]
  @should_expire_after_second = opts[:expire].to_i if opts[:expire]
end