/
plugin.rb
87 lines (79 loc) · 2.47 KB
/
plugin.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Stasis
class Plugin
class << self
def inherited(subclass)
@plugins ||= []
@plugins << subclass
end
def plugins
(@plugins || []).dup
end
# `Hash` -- Keys are the "bind as" method names and values are the actual method
# names on the `Plugin` instance.
attr_accessor :_methods
# `Fixnum` -- The execution priority for this plugin (defaults to 0).
def _priority; @priority || 0; end
# The methods in this `Array` essentially all take the same kind of parameters.
# Either a `Hash` or an `Array` of method names. No matter what, the input is
# converted to a `Hash` (see `_methods`).
%w(
action_method
after_all
after_render
after_write
before_all
before_render
controller_method
reset
).each do |method|
method = method.to_sym
# Define method on the `Plugin` class.
define_method(method) do |*methods|
# Set defaults on the `_` class variable.
self._methods ||= {}
self._methods[method] ||= {}
# If passing a `Hash`...
if methods[0].is_a?(::Hash)
self._methods[method].merge!(methods[0])
# If passing an `Array`...
else
# Generate `Hash` from `Array`.
methods = methods.inject({}) do |hash, m|
hash[m] = m
hash
end
self._methods[method].merge!(methods)
end
end
end
# Class method to set priority on the `Plugin`.
def priority(number)
@priority = number
end
end
# Helper method provided for built-in Stasis plugins. Returns a boolean value denoting
# whether or not a path is within another path.
def _within?(within_path, path=@stasis.path)
if within_path && path
dir = File.dirname(within_path)
path[0..dir.length-1] == dir
else
true
end
end
# Helper method provided for built-in Stasis plugins. Returns an `Array` of values
# of a `Hash` whose keys are `nil`, a literal match, or a pattern match.
def _match_key?(hash, match_key)
hash.inject([]) do |array, (key, value)|
if key.nil?
array << value
elsif key.is_a?(::String) && key == match_key
array << value
elsif key.is_a?(::Regexp) && key =~ match_key
array << value
end
array
end
end
end
end