through 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/through.rb +89 -0
  3. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2dabf20de968072039204ec60b672f63e7bc0a19
4
+ data.tar.gz: 0307472110e6924be59f389b7fadc6ba47b9ec45
5
+ SHA512:
6
+ metadata.gz: cf18b6ba16c408020e0fc97f402b5a967f0f4f7c648e9c0d1dec25a14f88340f81191961a81a95c426207fc0995a256b47f092874cdbfc75d8a0c9190fab4ee0
7
+ data.tar.gz: ee91e3cbeb3829c0f2d2241217811f1137b49d4cc526e972dc2ff1ad21bb0e19077ddcec42197821d344aefe3039941a9f3f8d8253996fe3ddaf30882c10a9c5
data/lib/through.rb ADDED
@@ -0,0 +1,89 @@
1
+ require "through/version"
2
+
3
+ module Through
4
+ class Pipe
5
+ def initialize(object, context = {})
6
+ @object = object
7
+ @context = context
8
+ end
9
+
10
+ def self.pipes_with
11
+ @pipes_with ||= {}
12
+ end
13
+
14
+ def self.pipes_without
15
+ @pipes_without ||= {}
16
+ end
17
+
18
+ def self.pipes
19
+ @pipes ||= []
20
+ end
21
+
22
+ def self.pipe_with(name, options = {})
23
+ pipes_with[name] = options.merge({
24
+ scope: -> (query, arg, context) { yield(query, arg, context) }
25
+ })
26
+ end
27
+
28
+ def self.pipe_without(name, options = {})
29
+ pipes_without[name] = options.merge({
30
+ scope: -> (query, arg, context) { yield(query, arg, context) }
31
+ })
32
+ end
33
+
34
+ def self.pipe(options = {})
35
+ pipes << options.merge({
36
+ scope: -> (query, context) { yield(query, context) }
37
+ })
38
+ end
39
+
40
+ def should_filter_pipe?(filter)
41
+ if (filter.has_key?(:if))
42
+ filter[:if].call(@context)
43
+ else
44
+ true
45
+ end
46
+ end
47
+
48
+ def should_filter?(filter, params)
49
+ if (filter.has_key?(:if))
50
+ filter[:if].call(params, @context)
51
+ else
52
+ true
53
+ end
54
+ end
55
+
56
+ def through_pipe
57
+ self.class.pipes.each do |context_filter|
58
+ if (self.should_filter_pipe?(context_filter))
59
+ @object = context_filter[:scope].call(@object, @context)
60
+ end
61
+ end
62
+ end
63
+
64
+ def through_with(params)
65
+ self.class.pipes_with.each do |without|
66
+ key, value = without
67
+ if (params.has_key?(key) && self.should_filter?(value, params[key]))
68
+ @object = value[:scope].call(@object, params[key], @context)
69
+ end
70
+ end
71
+ end
72
+
73
+ def through_without(params)
74
+ self.class.pipes_without.each do |without|
75
+ key, value = without
76
+ if (!params.has_key?(key) && self.should_filter?(value, params))
77
+ @object = value[:scope].call(@object, params, @context)
78
+ end
79
+ end
80
+ end
81
+
82
+ def through(params = {})
83
+ self.through_pipe
84
+ self.through_without(params)
85
+ self.through_with(params)
86
+ @object
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: through
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Boisvert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: This gem has no official purpose, it's a pipeline, pipe anything you
56
+ want through it from string to ActiveRecord query
57
+ email:
58
+ - nicolas@rumandcode.io
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/through.rb
64
+ homepage: https://github.com/rum-and-code/through
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.5.2.3
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A gem that helps you write pipeline
88
+ test_files: []