Class: LedgerSync::Test::RecordCollection
- Inherits:
-
Object
- Object
- LedgerSync::Test::RecordCollection
- Defined in:
- lib/ledger_sync/test/support/record_collection.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#record_class ⇒ Object
readonly
Returns the value of attribute record_class.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
- #all ⇒ Object
-
#initialize(args = {}) ⇒ RecordCollection
constructor
A new instance of RecordCollection.
Constructor Details
#initialize(args = {}) ⇒ RecordCollection
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ledger_sync/test/support/record_collection.rb', line 22 def initialize(args = {}) @dir = args.fetch(:dir) @record_class = args.fetch(:record_class, Record) @records = {} # Process json files Gem.find_files(File.join(dir, '*.json')).map do |file_path| record = File.basename(file_path, '.json').to_sym @records[record] = record_class.new( hash: JSON.parse(File.read(file_path)), path: file_path, record: record ) self.class.define_method(record) do records[record] end end # Process directories Dir.chdir(dir) do Dir.glob('*').select { |f| File.directory? f }.each do |sub_dir| sub_dir_path = File.join(dir, sub_dir) next if Gem.find_files(File.join(sub_dir_path, '**/*.json')).empty? @records[sub_dir.to_sym] = RecordCollection.new(dir: sub_dir_path) self.class.define_method(sub_dir) do records[sub_dir.to_sym] end end end end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
20 21 22 |
# File 'lib/ledger_sync/test/support/record_collection.rb', line 20 def dir @dir end |
#record_class ⇒ Object (readonly)
Returns the value of attribute record_class.
20 21 22 |
# File 'lib/ledger_sync/test/support/record_collection.rb', line 20 def record_class @record_class end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
20 21 22 |
# File 'lib/ledger_sync/test/support/record_collection.rb', line 20 def records @records end |
Instance Method Details
#all ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ledger_sync/test/support/record_collection.rb', line 55 def all @all ||= begin ret = {} records.each do |k, v| if v.is_a?(self.class) v.all.each do |sub_k, sub_v| ret[[k, sub_k].join('/').to_s] = sub_v end else ret[k.to_s] = v end end ret end end |