missile_emitter 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/README.md +10 -14
- data/lib/missile_emitter/version.rb +1 -1
- data/lib/missile_emitter.rb +4 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46ab698e7bba2e8af0eaf93d8a9882ff522a5acca5d17de5308737558a0476c0
|
4
|
+
data.tar.gz: d63ad0e9764163f14abf3a326c528be00963b7ab95bd6775387e4c9e27801b95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c22c26896d40b8d2e5fae943de2d20d67bf96b43b349aefc3e2e447513403f2d8aef593da3b1334a8112c61236cde307682a16d7883b2b6d4330f702183fd4f0
|
7
|
+
data.tar.gz: 0b23e86509f97ca64343e1c1b44bae62b99c5508451c09b0f85cd13455be774a438f8a6ebea8b932985b1f8cab471caa3fbc20ee23fb70773a798013b25b0b8a
|
data/README.md
CHANGED
@@ -102,22 +102,20 @@ me.age # => 36
|
|
102
102
|
|
103
103
|
```ruby
|
104
104
|
module Searchable
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
included do
|
109
|
-
# 使用 class attribute 保存搜索条件
|
110
|
-
class_attribute :conditions, {}
|
111
|
-
end
|
105
|
+
# 搜索条件(klass => {keyword: block})
|
106
|
+
# eg. {Person => {name_like: proc, older_than: proc}}
|
107
|
+
conditions = {}.with_indifferent_access
|
112
108
|
|
113
109
|
MissileEmitter do |klass, key, *, &block|
|
114
|
-
|
115
|
-
|
110
|
+
conditions[klass] ||= {}
|
111
|
+
conditions[klass][key] = block
|
116
112
|
end
|
117
113
|
|
118
|
-
|
114
|
+
extend ActiveSupport::Concern
|
115
|
+
|
116
|
+
included do
|
119
117
|
|
120
|
-
|
118
|
+
define_singleton_method :search do |hash|
|
121
119
|
hash.reduce all do |relation, (key, value)|
|
122
120
|
# Just for fun :)
|
123
121
|
relation = relation.extending do
|
@@ -126,7 +124,7 @@ module Searchable
|
|
126
124
|
|
127
125
|
if value.blank?
|
128
126
|
relation
|
129
|
-
elsif filter =
|
127
|
+
elsif filter = conditions.fetch(self, {})[key]
|
130
128
|
relation.instance_exec(value, &filter)
|
131
129
|
elsif column_names.include?(key)
|
132
130
|
relation.where key => value
|
@@ -136,8 +134,6 @@ module Searchable
|
|
136
134
|
end
|
137
135
|
end
|
138
136
|
|
139
|
-
end
|
140
|
-
|
141
137
|
end
|
142
138
|
```
|
143
139
|
|
data/lib/missile_emitter.rb
CHANGED