-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkflow.rb
66 lines (55 loc) · 1.43 KB
/
workflow.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'delegate'
$LOAD_PATH.unshift(File.expand_path('../vendor/bundle', __FILE__))
require 'bundler/setup'
require 'alphred'
require 'faker'
module Workflow
class Faker
def items
Alphred::Items[
*faker_methods.map { |method|
result = method.call rescue next # Ignore missing translations
klass = method.owner.to_s[/#<Class:(.*)>/, 1]
klass_short = klass.split('::').last.downcase
query = [klass_short, method.name]
Item.new(query, result)
}.compact
]
end
private
def faker_klasses
::Faker.constants
.reject {|c| c == :Config }
.map {|c| ::Faker.const_get(c) }
.select {|c| Class === c}
end
def faker_methods
faker_klasses.flat_map { |klass|
klass.singleton_methods(false)
.map {|m| klass.method(m) }
.select {|m| [-1, 0].include?(m.arity) }
}
end
end
class Item < SimpleDelegator
def initialize(query, result)
query_string = query.join(' ')
title = query[0]
title << " - #{query[1..-1].join(' ')}" if query.size > 1
super(
Alphred::Item.new(
uid: query_string,
arg: result,
autocomplete: query_string,
title: title,
subtitle: result,
icon: 'icon.png',
)
)
end
end
end
if __FILE__ == $0
workflow = Workflow::Faker.new
puts workflow.items.to_json
end