Class: RecordScanner
- Inherits:
-
Object
- Object
- RecordScanner
- Defined in:
- lib/test_fixture_boy/record_scanner.rb
Instance Method Summary collapse
- #except(attrs) ⇒ Object
-
#initialize ⇒ RecordScanner
constructor
A new instance of RecordScanner.
- #log(level, msg, retval) ⇒ Object
- #logger ⇒ Object
- #print_yaml ⇒ Object
- #scan(clear_cache = false) ⇒ Object
- #select(attrs) ⇒ Object
- #symbolize_attrs(attrs) ⇒ Object
Constructor Details
#initialize ⇒ RecordScanner
Returns a new instance of RecordScanner.
5 6 7 8 |
# File 'lib/test_fixture_boy/record_scanner.rb', line 5 def initialize @cache = HashWithIndifferentAccess.new @filter = {} end |
Instance Method Details
#except(attrs) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/test_fixture_boy/record_scanner.rb', line 42 def except(attrs) attributes = symbolize_attrs attrs @filter = { except: attributes } puts @filter self end |
#log(level, msg, retval) ⇒ Object
54 55 56 57 |
# File 'lib/test_fixture_boy/record_scanner.rb', line 54 def log(level, msg, retval) logger.send(level, msg) retval end |
#logger ⇒ Object
59 60 61 62 63 |
# File 'lib/test_fixture_boy/record_scanner.rb', line 59 def logger return @logger if @logger @logger = Logger.new(STDOUT) @logger.level = Logger::WARN end |
#print_yaml ⇒ Object
28 29 30 31 32 33 |
# File 'lib/test_fixture_boy/record_scanner.rb', line 28 def print_yaml @cache.inject({}) do |h, (model, records)| h[model] = records.map(&:to_yaml) h end end |
#scan(clear_cache = false) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/test_fixture_boy/record_scanner.rb', line 10 def scan(clear_cache = false) @cache.clear if clear_cache query_results = yield return log(:warn, 'No query results.', @cache) unless query_results && query_results.present? records = query_results.is_a?(Array) ? query_results : [query_results] return log(:error, 'Not active records.', @cache) unless records.first.class < ActiveRecord::Base model_name = records.first.class.name @cache[model_name] ||= [] @cache[model_name] = records.map(&:attributes) @cache[model_name].map!{ |h| h.slice(*@filter[:select]) } if @filter[:select] @cache[model_name].map!{ |h| h.except(*@filter[:except]) } if @filter[:except] @filter = {} @cache end |
#select(attrs) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/test_fixture_boy/record_scanner.rb', line 35 def select(attrs) attributes = symbolize_attrs attrs @filter = { select: attributes } puts @filter self end |
#symbolize_attrs(attrs) ⇒ Object
49 50 51 52 |
# File 'lib/test_fixture_boy/record_scanner.rb', line 49 def symbolize_attrs(attrs) attrs = [attrs] unless attrs.is_a? Array attrs.map(&:to_sym) end |