Sha256: 4d8dd6ba90577454bd10efc6e6d85d786944e0c42b7e2556ec83ef49f68b2025

Contents?: true

Size: 1.68 KB

Versions: 10

Compression:

Stored size: 1.68 KB

Contents

require "tempfile"

module FixtureFox
  class Idr
    # Qualified names of controlled tables
    def tables() @tables_hash.keys end

    # List of materialized views that depends on the tables. Assigned by the analyzer
    # FIXME: Is this in use?
    attr_accessor :materialized_views
    
    # Data as a hash from schema to table to id to record to field to value.
    # Ie. { "schema" => { "table" => { 1 => { id: 1, name: "Alice" } } } }
    attr_reader :data

    # Map from qualified table name to last used ID. FIXME: Unused?
    attr_reader :ids

    def initialize
      @tables_hash = {}
      @data = {}
    end

    # Number of records
    def count()
      count = 0
      @data.each { |_, tables| tables.each { |_, records| count += records.size } }
      count
    end

    # Add a field to the data representation. If field and value are nil, an
    # empty record will be created. If id is also nil, an empty table is
    # created
    #
    def put(schema, table, id = nil, field = nil, value = nil)
#     puts "Idr.put(#{schema}, #{table}, #{id.inspect}, #{field.inspect}, #{value.inspect})"
      !id.nil? || field.nil? or raise ArgumentError
      raise if table.to_s =~ /[A-Z]/
      uid = "#{schema}.#{table}"
      @tables_hash[uid] = true
      return if id.nil?
      tuple = ((@data[schema] ||= {})[table] ||= {})[id] ||= {id: id}
      tuple[field.to_sym] = value if field
    end

    alias_method :to_h, :data

    def dump
      data.sort_by(&:first).each { |schema, tables|
        puts schema
        tables.each { |table, records| 
          puts "  #{table}"
          records.each { |id, fields|
            puts "    #{fields.inspect}"
          }
        }
      }
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fixture_fox-0.2.6 lib/fixture_fox/idr.rb
fixture_fox-0.2.5 lib/fixture_fox/idr.rb
fixture_fox-0.2.4 lib/fixture_fox/idr.rb
fixture_fox-0.2.3 lib/fixture_fox/idr.rb
fixture_fox-0.2.2 lib/fixture_fox/idr.rb
fixture_fox-0.2.1 lib/fixture_fox/idr.rb
fixture_fox-0.2.0 lib/fixture_fox/idr.rb
fixture_fox-0.1.3 lib/fixture_fox/idr.rb
fixture_fox-0.1.2 lib/fixture_fox/idr.rb
fixture_fox-0.1.1 lib/fixture_fox/idr.rb