Class: Through::Pipe
- Inherits:
-
Object
- Object
- Through::Pipe
- Defined in:
- lib/through.rb
Class Method Summary collapse
- .pipe(options = {}) ⇒ Object
- .pipe_with(name, options = {}) ⇒ Object
- .pipe_without(name, options = {}) ⇒ Object
- .pipes ⇒ Object
- .pipes_with ⇒ Object
- .pipes_without ⇒ Object
Instance Method Summary collapse
-
#initialize(object, context = {}) ⇒ Pipe
constructor
A new instance of Pipe.
- #should_filter?(filter, params) ⇒ Boolean
- #should_filter_pipe?(filter) ⇒ Boolean
- #through(params = {}) ⇒ Object
- #through_pipe ⇒ Object
- #through_with(params) ⇒ Object
- #through_without(params) ⇒ Object
Constructor Details
#initialize(object, context = {}) ⇒ Pipe
Returns a new instance of Pipe.
5 6 7 8 |
# File 'lib/through.rb', line 5 def initialize(object, context = {}) @object = object @context = context end |
Class Method Details
.pipe(options = {}) ⇒ Object
34 35 36 37 38 |
# File 'lib/through.rb', line 34 def self.pipe( = {}) pipes << .merge({ scope: -> (query, context) { yield(query, context) } }) end |
.pipe_with(name, options = {}) ⇒ Object
22 23 24 25 26 |
# File 'lib/through.rb', line 22 def self.pipe_with(name, = {}) pipes_with[name] = .merge({ scope: -> (query, arg, context) { yield(query, arg, context) } }) end |
.pipe_without(name, options = {}) ⇒ Object
28 29 30 31 32 |
# File 'lib/through.rb', line 28 def self.pipe_without(name, = {}) pipes_without[name] = .merge({ scope: -> (query, arg, context) { yield(query, arg, context) } }) end |
.pipes ⇒ Object
18 19 20 |
# File 'lib/through.rb', line 18 def self.pipes @pipes ||= [] end |
.pipes_with ⇒ Object
10 11 12 |
# File 'lib/through.rb', line 10 def self.pipes_with @pipes_with ||= {} end |
.pipes_without ⇒ Object
14 15 16 |
# File 'lib/through.rb', line 14 def self.pipes_without @pipes_without ||= {} end |
Instance Method Details
#should_filter?(filter, params) ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'lib/through.rb', line 48 def should_filter?(filter, params) if (filter.has_key?(:if)) filter[:if].call(params, @context) else true end end |
#should_filter_pipe?(filter) ⇒ Boolean
40 41 42 43 44 45 46 |
# File 'lib/through.rb', line 40 def should_filter_pipe?(filter) if (filter.has_key?(:if)) filter[:if].call(@context) else true end end |
#through(params = {}) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/through.rb', line 82 def through(params = {}) self.through_pipe self.through_without(params) self.through_with(params) @object end |
#through_pipe ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/through.rb', line 56 def through_pipe self.class.pipes.each do |context_filter| if (self.should_filter_pipe?(context_filter)) @object = context_filter[:scope].call(@object, @context) end end end |
#through_with(params) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/through.rb', line 64 def through_with(params) self.class.pipes_with.each do |without| key, value = without if (params.has_key?(key) && self.should_filter?(value, params[key])) @object = value[:scope].call(@object, params[key], @context) end end end |
#through_without(params) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/through.rb', line 73 def through_without(params) self.class.pipes_without.each do |without| key, value = without if (!params.has_key?(key) && self.should_filter?(value, params)) @object = value[:scope].call(@object, params, @context) end end end |