Class: LedgerSync::Test::RecordCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger_sync/test/support/record_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ RecordCollection

Returns a new instance of 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.open(file_path).read),
      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

#dirObject (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_classObject (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

#recordsObject (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

#allObject



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