Class: LikeQuery::Collect
- Inherits:
-
Object
- Object
- LikeQuery::Collect
- Defined in:
- lib/like_query/collect.rb
Instance Method Summary collapse
- #collect(output_schema = nil, limit: nil, parent: nil, image: nil, url: nil, &block) ⇒ Object
- #generate_hash ⇒ Object
- #generate_json ⇒ Object
-
#initialize(limit = nil) ⇒ Collect
constructor
A new instance of Collect.
- #set_schema(model, schema = nil, url: nil, no_action: false) ⇒ Object
Constructor Details
#initialize(limit = nil) ⇒ Collect
Returns a new instance of Collect.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/like_query/collect.rb', line 3 def initialize(limit = nil) if limit @limit = limit elsif Rails.configuration.x.like_query.limit @limit = Rails.configuration.x.like_query.limit else @limit = nil end @length = 0 @data = {} @overflow = false @columns_count = 0 @sub_records_columns_count = 0 @image = false @start_time = Time.now @schemes = {} @images = {} @urls = {} @no_action = {} end |
Instance Method Details
#collect(output_schema = nil, limit: nil, parent: nil, image: nil, url: nil, &block) ⇒ Object
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 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/like_query/collect.rb', line 30 def collect(output_schema = nil, limit: nil, parent: nil, image: nil, url: nil, &block) _limit = (limit ? (@limit && @limit < limit ? @limit : limit) : @limit) return false if @length >= _limit length = 0 recs = yield.includes(parent).limit(_limit) scm = (output_schema.present? ? output_schema : recs.like_query_schema) if scm.present? @schemes[recs.klass.to_s] = schema_to_hash(scm) end model_name = recs.klass.to_s schema = @schemes[model_name] || schema_to_hash(nil) _img = image || schema[:image] if _img.present? @images[model_name] = _img @image = true end _url = url || @urls[model_name] if url.present? @urls[model_name] = url end if parent parent_assoc = recs.klass.reflect_on_association(parent) if !parent_assoc raise "parent «#{parent}» is not a valid association" end end recs.each do |rec| if @length >= _limit @overflow = true break else if parent parent_record = rec.send(parent) r = record_to_hash(rec, schema, image, parent_record, url: _url) parent_class_name = parent_record.class.to_s parent_key = "#{parent_class_name}#{parent_record.id}" unless @data[parent_key] parent_schema = @schemes[parent_class_name] unless parent_schema Rails.logger.debug("WARNING: NO SCHEMA GIVEN FOR «#{parent_class_name}»") parent_schema = schema_to_hash(nil) end @data[parent_key] = record_to_hash(parent_record, parent_schema, @images[parent_class_name], url: @urls[parent_class_name]) @length += 1 end @data[parent_key][:children] ||= [] @data[parent_key][:children].push(r) else r = record_to_hash(rec, schema, image, url: _url) @data["#{rec.class}#{rec.id}"] = r end c = (@image ? 1 : 0) + r[:values].to_a.length @columns_count = c if c > @columns_count @length += 1 length += 1 end end true end |
#generate_hash ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/like_query/collect.rb', line 101 def generate_hash data = @data.map { |_, v| v } { data: data, length: @length, overflow: @overflow, columns_count: @columns_count, sub_records_columns_count: @sub_records_columns_count, image: @image, # sub_records_image: @sub_records_image, time: Time.now - @start_time } end |
#generate_json ⇒ Object
115 116 117 |
# File 'lib/like_query/collect.rb', line 115 def generate_json generate_hash.to_json end |
#set_schema(model, schema = nil, url: nil, no_action: false) ⇒ Object
24 25 26 27 28 |
# File 'lib/like_query/collect.rb', line 24 def set_schema(model, schema = nil, url: nil, no_action: false) @schemes[model.to_s] = schema_to_hash(schema) @urls[model.to_s] = url if url @no_action[model.to_s] = no_action end |