card 1.18.4 → 1.18.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.yardoc/checksums +263 -240
  3. data/.yardoc/object_types +0 -0
  4. data/.yardoc/objects/root.dat +0 -0
  5. data/VERSION +1 -1
  6. data/card.gemspec +5 -2
  7. data/config/initializers/01_core_extensions/array.rb +9 -0
  8. data/config/initializers/01_core_extensions/hash.rb +57 -0
  9. data/config/initializers/01_core_extensions/module.rb +14 -0
  10. data/config/initializers/01_core_extensions/object.rb +43 -0
  11. data/config/initializers/02_extensions/kaminari.rb +38 -0
  12. data/config/initializers/core_extensions.rb +24 -0
  13. data/config/initializers/extensions.rb +3 -0
  14. data/lib/card.rb +15 -10
  15. data/lib/card/director_register.rb +7 -0
  16. data/lib/card/loader.rb +29 -39
  17. data/lib/card/migration.rb +14 -0
  18. data/lib/card/query.rb +17 -128
  19. data/lib/card/query/attributes.rb +2 -244
  20. data/lib/card/query/conjunctions.rb +36 -0
  21. data/lib/card/query/helpers.rb +72 -0
  22. data/lib/card/query/interpretation.rb +124 -0
  23. data/lib/card/query/relational_attributes.rb +95 -0
  24. data/lib/card/query/sorting.rb +52 -0
  25. data/lib/card/set.rb +73 -362
  26. data/lib/card/set/format.rb +122 -0
  27. data/lib/card/set/helpers.rb +100 -0
  28. data/lib/card/set/loader.rb +98 -0
  29. data/lib/card/set/trait.rb +5 -2
  30. data/lib/card/subcards.rb +38 -12
  31. data/lib/cardio.rb +1 -1
  32. data/lib/generators/card/migration/templates/card_migration.erb +1 -1
  33. data/mod/01_core/chunk/include.rb +13 -1
  34. data/mod/01_core/set/all/event.rb +14 -0
  35. data/mod/01_core/set/all/fetch.rb +21 -7
  36. data/mod/01_core/set/all/states.rb +3 -1
  37. data/mod/01_core/set/all/subcards.rb +4 -2
  38. data/mod/01_core/spec/set/all/fetch_spec.rb +14 -1
  39. data/mod/02_basic_types/set/abstract/code_file.rb +6 -0
  40. data/mod/04_settings/set/type/setting.rb +10 -0
  41. data/mod/05_standard/set/all/rich_html/wrapper.rb +1 -1
  42. data/mod/05_standard/set/self/codenames.rb +0 -0
  43. data/mod/05_standard/spec/chunk/include_spec.rb +10 -10
  44. data/spec/config/initializers/core_extensions_spec.rb +15 -0
  45. data/spec/lib/card/stage_director_spec.rb +25 -1
  46. metadata +25 -8
  47. data/config/initializers/01_init_ruby_extensions.rb +0 -3
  48. data/lib/card/core_ext.rb +0 -107
@@ -2,108 +2,13 @@
2
2
  class Card
3
3
  class Query
4
4
  module Attributes
5
- SORT_JOIN_TO_ITEM_MAP = { left: 'left_id', right: 'right_id' }.freeze
6
-
7
- # ~~~~~~ RELATIONAL
8
-
9
- def type val
10
- restrict :type_id, val
11
- end
12
-
13
- def part val
14
- right_val = val.is_a?(Integer) ? val : val.clone
15
- any(left: val, right: right_val)
16
- end
17
-
18
- def left val
19
- restrict :left_id, val
20
- end
21
-
22
- def right val
23
- restrict :right_id, val
24
- end
25
-
26
- def editor_of val
27
- act_join = Join.new(
28
- from: self,
29
- to: ['card_acts', "a#{table_id true}", 'actor_id']
30
- )
31
- joins << act_join
32
- action_join = Join.new(
33
- from: act_join,
34
- to: ['card_actions', "an#{table_id true}", 'card_act_id'],
35
- superjoin: act_join
36
- )
37
- join_cards val, from: action_join, from_field: 'card_id'
38
- end
39
-
40
- def edited_by val
41
- action_join = Join.new(
42
- from: self,
43
- to: ['card_actions', "an#{table_id true}", 'card_id']
44
- )
45
- joins << action_join
46
- act_join = Join.new(
47
- from: action_join,
48
- from_field: 'card_act_id',
49
- to: ['card_acts', "a#{table_id true}"]
50
- )
51
- join_cards val, from: act_join, from_field: 'actor_id'
52
- end
53
-
54
- def last_editor_of val
55
- join_cards val, to_field: 'updater_id'
56
- end
57
-
58
- def last_edited_by val
59
- restrict :updater_id, val
60
- end
61
-
62
- def creator_of val
63
- join_cards val, to_field: 'creator_id'
64
- end
65
-
66
- def created_by val
67
- restrict :creator_id, val
68
- end
69
-
70
- def member_of val
71
- interpret right_plus: [RolesID, refer_to: val]
72
- end
73
-
74
- def member val
75
- interpret referred_to_by: { left: val, right: RolesID }
76
- end
77
-
78
- # ~~~~~~ PLUS RELATIONAL
79
-
80
- def left_plus val
81
- junction val, :left, :right_id
82
- end
83
-
84
- def right_plus val
85
- junction val, :right, :left_id
86
- end
87
-
88
- def plus val
89
- any(left_plus: val, right_plus: val.deep_clone)
90
- end
91
-
92
- def junction val, side, to_field
93
- part_clause, junction_clause = val.is_a?(Array) ? val : [val, {}]
94
- junction_val = clause_to_hash(junction_clause).merge side => part_clause
95
- join_cards junction_val, to_field: to_field
96
- end
97
-
98
- # ~~~~~~ SPECIAL
99
-
100
5
  def found_by val
101
6
  found_by_cards(val).compact.each do |c|
102
7
  if c && [SearchTypeID, SetID].include?(c.type_id)
103
8
  # FIXME: - move this check to set mods!
104
9
 
105
10
  subquery(
106
- c.get_query.merge unjoined: true, context: c.name
11
+ c.get_query.merge(unjoined: true, context: c.name)
107
12
  )
108
13
  else
109
14
  raise BadQuery,
@@ -125,7 +30,7 @@ class Card
125
30
 
126
31
  def match val
127
32
  cxn, val = match_prep val
128
- val.gsub! /[^#{Card::Name::OK4KEY_RE}]+/, ' '
33
+ val.gsub!(/[^#{Card::Name::OK4KEY_RE}]+/, ' ')
129
34
  return nil if val.strip.empty?
130
35
 
131
36
  val_list = val.split(/\s+/).map do |v|
@@ -156,153 +61,6 @@ class Card
156
61
  Rails.logger.info 'using DEPRECATED extension_type in WQL'
157
62
  interpret right_plus: AccountID
158
63
  end
159
-
160
- # ATTRIBUTE HELPERS
161
-
162
- def join_references key, val
163
- r = Reference.new(key, val, self)
164
- refjoin = Join.new(from: self, to: r, to_field: r.infield)
165
- joins << refjoin
166
- restrict_reference r, refjoin if r.cardquery
167
- r.conditions.each do |condition|
168
- refjoin.conditions << "#{r.table_alias}.#{condition}"
169
- end
170
- end
171
-
172
- def restrict_reference ref, refjoin
173
- val = ref.cardquery
174
- if (id = id_from_val(val))
175
- add_condition "#{ref.table_alias}.#{ref.outfield} = #{id}"
176
- else
177
- join_cards val, from: refjoin, from_field: ref.outfield
178
- end
179
- end
180
-
181
- def conjunction val
182
- return unless [String, Symbol].member? val.class
183
- CONJUNCTIONS[val.to_sym]
184
- end
185
-
186
- def sort val
187
- return nil if @superquery
188
- sort_field = val[:return] || 'db_content'
189
- item = val.delete(:item) || 'left'
190
-
191
- if sort_field == 'count'
192
- sort_by_count val, item
193
- elsif (join_field = SORT_JOIN_TO_ITEM_MAP[item.to_sym])
194
- sq = join_cards(val, to_field: join_field,
195
- side: 'LEFT',
196
- conditions_on_join: true)
197
- @mods[:sort] ||= "#{sq.table_alias}.#{sort_field}"
198
- else
199
- raise BadQuery, "sort item: #{item} not yet implemented"
200
- end
201
- end
202
-
203
- # EXPERIMENTAL!
204
- def sort_by_count val, item
205
- if item == 'referred_to'
206
- @mods[:sort] = 'coalesce(count,0)' # needed for postgres
207
- cs = Query.new(
208
- return: 'coalesce(count(*), 0) as count',
209
- group: 'sort_join_field',
210
- superquery: self
211
- )
212
- subselect = Query.new val.merge(return: 'id', superquery: self)
213
- cs.add_condition "referer_id in (#{subselect.sql})"
214
- # FIXME: - SQL generated before SQL phase
215
- cs.joins << Join.new(
216
- from: cs,
217
- to: %w(card_references wr referee_id)
218
- )
219
- cs.mods[:sort_join_field] = "#{cs.table_alias}.id as sort_join_field"
220
- # HACK!
221
-
222
- joins << Join.new(
223
- from: self,
224
- to: [cs, 'srtbl', 'sort_join_field']
225
- )
226
- else
227
- raise BadQuery, "count with item: #{item} not yet implemented"
228
- end
229
- end
230
-
231
- def table_alias
232
- @table_alias ||= begin
233
- if @unjoined
234
- @superquery.table_alias
235
- else
236
- "c#{table_id}"
237
- end
238
- end
239
- end
240
-
241
- def table_id force=false
242
- if force
243
- tick_table_seq!
244
- else
245
- @table_id ||= tick_table_seq!
246
- end
247
- end
248
-
249
- def tick_table_seq!
250
- root.table_seq = root.table_seq.to_i + 1
251
- end
252
-
253
- def join_cards val, opts={}
254
- conditions_on_join = opts.delete :conditions_on_join
255
- s = subquery
256
- join_opts = { from: self, to: s }.merge opts
257
- card_join = Join.new join_opts
258
- joins << card_join unless opts[:from].is_a? Join
259
- s.conditions_on_join = card_join if conditions_on_join
260
- s.interpret val
261
- s
262
- end
263
-
264
- # ~~~~~~~ CONJUNCTION
265
-
266
- def all val
267
- conjoin val, :and
268
- end
269
- alias and all
270
-
271
- def any val
272
- conjoin val, :or
273
- end
274
- alias or any
275
- alias in any
276
-
277
- def conjoin val, conj
278
- sq = subquery unjoined: true, conj: conj
279
- unless val.is_a? Array
280
- val = clause_to_hash(val).map { |key, value| { key => value } }
281
- end
282
- val.each do |val_item|
283
- sq.interpret val_item
284
- end
285
- end
286
-
287
- def not val
288
- notjoin = join_cards val, conditions_on_join: true, side: 'LEFT'
289
- add_condition "#{notjoin.table_alias}.id is null"
290
- end
291
-
292
- def restrict id_field, val
293
- if (id = id_from_val(val))
294
- interpret id_field => id
295
- else
296
- join_cards val, from_field: id_field
297
- end
298
- end
299
-
300
- def id_from_val val
301
- case val
302
- when Integer then val
303
- when String then Card.fetch_id(val)
304
- end
305
- end
306
64
  end
307
65
  end
308
66
  end
@@ -0,0 +1,36 @@
1
+ class Card
2
+ class Query
3
+ module Conjunctions
4
+ def all val
5
+ conjoin val, :and
6
+ end
7
+ alias and all
8
+
9
+ def any val
10
+ conjoin val, :or
11
+ end
12
+ alias or any
13
+ alias in any
14
+
15
+ def conjoin val, conj
16
+ sq = subquery unjoined: true, conj: conj
17
+ unless val.is_a? Array
18
+ val = clause_to_hash(val).map { |key, value| { key => value } }
19
+ end
20
+ val.each do |val_item|
21
+ sq.interpret val_item
22
+ end
23
+ end
24
+
25
+ def not val
26
+ notjoin = join_cards val, conditions_on_join: true, side: 'LEFT'
27
+ add_condition "#{notjoin.table_alias}.id is null"
28
+ end
29
+
30
+ def conjunction val
31
+ return unless [String, Symbol].member? val.class
32
+ CONJUNCTIONS[val.to_sym]
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,72 @@
1
+ class Card
2
+ class Query
3
+ module Helpers
4
+ def restrict id_field, val
5
+ if (id = id_from_val(val))
6
+ interpret id_field => id
7
+ else
8
+ join_cards val, from_field: id_field
9
+ end
10
+ end
11
+
12
+ def restrict_reference ref, refjoin
13
+ val = ref.cardquery
14
+ if (id = id_from_val(val))
15
+ add_condition "#{ref.table_alias}.#{ref.outfield} = #{id}"
16
+ else
17
+ join_cards val, from: refjoin, from_field: ref.outfield
18
+ end
19
+ end
20
+
21
+ def join_references key, val
22
+ r = Reference.new(key, val, self)
23
+ refjoin = Join.new(from: self, to: r, to_field: r.infield)
24
+ joins << refjoin
25
+ restrict_reference r, refjoin if r.cardquery
26
+ r.conditions.each do |condition|
27
+ refjoin.conditions << "#{r.table_alias}.#{condition}"
28
+ end
29
+ end
30
+
31
+ def id_from_val val
32
+ case val
33
+ when Integer then val
34
+ when String then Card.fetch_id(val)
35
+ end
36
+ end
37
+
38
+ def table_alias
39
+ @table_alias ||= begin
40
+ if @unjoined
41
+ @superquery.table_alias
42
+ else
43
+ "c#{table_id}"
44
+ end
45
+ end
46
+ end
47
+
48
+ def table_id force=false
49
+ if force
50
+ tick_table_seq!
51
+ else
52
+ @table_id ||= tick_table_seq!
53
+ end
54
+ end
55
+
56
+ def tick_table_seq!
57
+ root.table_seq = root.table_seq.to_i + 1
58
+ end
59
+
60
+ def join_cards val, opts={}
61
+ conditions_on_join = opts.delete :conditions_on_join
62
+ s = subquery
63
+ join_opts = { from: self, to: s }.merge opts
64
+ card_join = Join.new join_opts
65
+ joins << card_join unless opts[:from].is_a? Join
66
+ s.conditions_on_join = card_join if conditions_on_join
67
+ s.interpret val
68
+ s
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,124 @@
1
+ class Card
2
+ class Query
3
+ module Interpretation
4
+ # normalize and extract meaning from a clause
5
+ # @param clause [Hash, String, Integer] statement or chunk thereof
6
+ def interpret clause
7
+ interpret_by_key normalize_clause(clause)
8
+ end
9
+
10
+ def interpret_by_key clause
11
+ clause.each do |key, val|
12
+ case
13
+ when OPERATORS.key?(key.to_s) && !ATTRIBUTES[key]
14
+ # eg "match" is both operator and attribute;
15
+ # interpret as attribute when "match" is key
16
+ interpret content: [key, val]
17
+ when MODIFIERS.key?(key) && !clause[key].is_a?(Hash)
18
+ # eg when "sort" is hash, it can have subqueries
19
+ # and must be interpreted like an attribute
20
+ @mods[key] = val.is_a?(Array) ? val : val.to_s
21
+ else
22
+ interpret_attributes key, val
23
+ end
24
+ end
25
+ end
26
+
27
+ def interpret_attributes key, val
28
+ case ATTRIBUTES[key]
29
+ when :basic then add_condition key, val
30
+ when :conjunction then send key, val
31
+ when :relational then relate key, val
32
+ when :special then relate key, val
33
+ when :ref_relational then relate key, val, method: :join_references
34
+ when :plus_relational then relate_compound key, val
35
+ when :ignore then # noop
36
+ else raise BadQuery, "Invalid attribute #{key}"
37
+ end
38
+ end
39
+
40
+ def normalize_clause clause
41
+ clause = clause_to_hash clause
42
+ clause.symbolize_keys!
43
+ clause.each do |key, val|
44
+ clause[key] = normalize_value val
45
+ end
46
+ clause
47
+ end
48
+
49
+ def clause_to_hash clause
50
+ case clause
51
+ when Hash then clause
52
+ when String then { key: clause.to_name.key }
53
+ when Integer then { id: clause }
54
+ else raise BadQuery, "Invalid query args #{clause.inspect}"
55
+ end
56
+ end
57
+
58
+ def normalize_value val
59
+ case val
60
+ when Integer, Float, Symbol, Hash then val
61
+ when String, SmartName then normalize_string_value val
62
+ when Array then val.map { |v| normalize_value v }
63
+ else raise BadQuery, "unknown WQL value type: #{val.class}"
64
+ end
65
+ end
66
+
67
+ def normalize_string_value val
68
+ case val.to_s
69
+ when /^\$(\w+)$/ # replace from @vars
70
+ @vars[Regexp.last_match[1].to_sym].to_s.strip
71
+ when /\b_/ # absolutize based on @context
72
+ val.to_name.to_absolute(context)
73
+ else
74
+ val
75
+ end
76
+ end
77
+
78
+ def add_condition *args
79
+ @conditions <<
80
+ if args.size > 1
81
+ [args.shift, Value.new(args.shift, self)]
82
+ else
83
+ args[0]
84
+ end
85
+ end
86
+
87
+ def relate_compound key, val
88
+ has_multiple_values =
89
+ val.is_a?(Array) &&
90
+ (val.first.is_a?(Array) || conjunction(val.first).present?)
91
+ relate key, val, multiple: has_multiple_values
92
+ end
93
+
94
+ def relate key, val, opts={}
95
+ multiple = opts[:multiple].nil? ? val.is_a?(Array) : opts[:multiple]
96
+ method = opts[:method] || :send
97
+ if multiple
98
+ relate_multi_value method, key, val
99
+ else
100
+ send method, key, val
101
+ end
102
+ end
103
+
104
+ def relate_multi_value method, key, val
105
+ conj = conjunction(val.first) ? conjunction(val.shift) : :and
106
+ if conj == current_conjunction
107
+ # same conjunction as container, no need for subcondition
108
+ val.each { |v| send method, key, v }
109
+ else
110
+ send conj, val.map { |v| { key => v } }
111
+ end
112
+ end
113
+
114
+ def current_conjunction
115
+ @mods[:conj].blank? ? :and : @mods[:conj]
116
+ end
117
+
118
+ def all_joins
119
+ @all_joins ||=
120
+ (joins + subqueries.select(&:unjoined).map(&:all_joins)).flatten
121
+ end
122
+ end
123
+ end
124
+ end