Sha256: 6509f9b48c3d1a241496f865fdacc3c96869d88b56cc26f6753bc1f70b9682a5

Contents?: true

Size: 1.68 KB

Versions: 6

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

6 entries across 6 versions & 1 rubygems

Version Path
fixture_fox-0.2.12 lib/fixture_fox/idr.rb
fixture_fox-0.2.11 lib/fixture_fox/idr.rb
fixture_fox-0.2.10 lib/fixture_fox/idr.rb
fixture_fox-0.2.9 lib/fixture_fox/idr.rb
fixture_fox-0.2.8 lib/fixture_fox/idr.rb
fixture_fox-0.2.7 lib/fixture_fox/idr.rb