Sha256: 95e7dec27d48e330b6fc644a3c838256efc6562e7aef5cb36ad13fc62daec86d

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

module SportDB

class Loader

## make models available in sportdb module by default with namespace
#  e.g. lets you use Team instead of Models::Team 
  include SportDB::Models


  def initialize( logger=nil )
    if logger.nil?
      @logger = Logger.new(STDOUT)
      @logger.level = Logger::INFO
    else
      @logger = logger
    end
  end

  attr_reader :logger


  def run( opts, args )

    args.each do |arg|
      name = arg     # File.basename( arg, '.*' )
      
      if opts.load?
        load_fixtures_builtin( name )
      else
        load_fixtures_with_include_path( name, opts.data_path )
      end
    end
    
  end # method run


  def load_fixtures_with_include_path( name, include_path )  # load from file system
    path = "#{include_path}/#{name}.rb"
 
    puts "*** loading data '#{name}' (#{path})..."

    ## nb: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
    ## - see sportdb/utils.rb
    code = File.read_utf8( path )
    
    load_fixtures_worker( code )
    
    Prop.create!( key: "db.#{fixture_name_to_prop_key(name)}.version", value: "file.rb.#{File.mtime(path).strftime('%Y.%m.%d')}" )
  end
  
  def load_fixtures_builtin( name ) # load from gem (built-in)
    path = "#{SportDB.data_path}/#{name}.rb"
 
    puts "*** loading data '#{name}' (#{path})..."

    code = File.read_utf8( path )
    
    load_fixtures_worker( code )

    ## for builtin fixtures use VERSION of gem

    Prop.create!( key: "db.#{fixture_name_to_prop_key(name)}.version", value: "sport.rb.#{SportDB::VERSION}" )
  end
  

private


  def load_fixtures_worker( code )
    
    self.class_eval( code )

    # NB: same as
    #
    # module SportDB
    #   include SportDB::Models
    #  <code here>
    # end
  end
  
end # class Loader
end # module SportDB

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sportdb-0.9.7 lib/sportdb/loader.rb
sportdb-0.9.6 lib/sportdb/loader.rb
sportdb-0.9.5 lib/sportdb/loader.rb
sportdb-0.9.4 lib/sportdb/loader.rb