Class: SportDb::EventMetaReader
- Inherits:
-
Object
- Object
- SportDb::EventMetaReader
- Includes:
- LogUtils::Logging, Model
- Defined in:
- lib/sportdb/readers/event_meta.rb
Constant Summary
Constants included from Model
Model::City, Model::Continent, Model::Country, Model::Person, Model::Prop, Model::State
Instance Attribute Summary collapse
-
#event ⇒ Object
readonly
returns event record; call read first.
-
#fixtures ⇒ Object
readonly
fixtures/sources entry from event config.
Class Method Summary collapse
- .from_file(path, more_attribs = {}) ⇒ Object
- .from_string(text, config, more_attribs = {}) ⇒ Object
- .from_zip(zip_file, entry_path, more_attribs = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(text, config, more_attribs = {}) ⇒ EventMetaReader
constructor
A new instance of EventMetaReader.
- #read ⇒ Object
Constructor Details
#initialize(text, config, more_attribs = {}) ⇒ EventMetaReader
Returns a new instance of EventMetaReader.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sportdb/readers/event_meta.rb', line 43 def initialize( text, config, more_attribs={} ) ## todo/fix: how to add opts={} ??? ## todo/check: more_attribs used ??? @text = text @more_attribs = more_attribs # name of event configuration (relative basename w/o path or string) @config = config @event = nil @fixtures = [] end |
Instance Attribute Details
#event ⇒ Object (readonly)
returns event record; call read first
14 15 16 |
# File 'lib/sportdb/readers/event_meta.rb', line 14 def event @event end |
#fixtures ⇒ Object (readonly)
fixtures/sources entry from event config
15 16 17 |
# File 'lib/sportdb/readers/event_meta.rb', line 15 def fixtures @fixtures end |
Class Method Details
.from_file(path, more_attribs = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/sportdb/readers/event_meta.rb', line 29 def self.from_file( path, more_attribs={} ) ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark) ## - see textutils/utils.rb text = File.read_utf8( path ) config = File.basename( path ) # name of .yml file self.from_string( text, config, more_attribs ) end |
.from_string(text, config, more_attribs = {}) ⇒ Object
39 40 41 |
# File 'lib/sportdb/readers/event_meta.rb', line 39 def self.from_string( text, config, more_attribs={} ) self.new( text, config, more_attribs ) end |
.from_zip(zip_file, entry_path, more_attribs = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sportdb/readers/event_meta.rb', line 17 def self.from_zip( zip_file, entry_path, more_attribs={} ) ## get text content from zip entry = zip_file.find_entry( entry_path ) text = entry.get_input_stream().read() text = text.force_encoding( Encoding::UTF_8 ) config = File.basename( entry_path ) # name of .yml file self.from_string( text, config, more_attribs ) end |
Instance Method Details
#read ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sportdb/readers/event_meta.rb', line 57 def read @event = nil # reset cached event rec @fixtures = [] # reset cached fixtures reader = HashReader.from_string( @text ) league = nil season = nil reader.each_typed do |key, value| ## puts "processing event attrib >>#{key}<< >>#{value}<<..." if key.downcase == 'league' ## note: allow league, League, etc. league_key = value.to_s.strip ## check if league_key includes uppercase letters (e.g. Deutsche Bundesliga and NOT de etc.) if league_key =~ /[A-Z]/ ## assume league name (NOT league key); try to lookup leauge key in database league = League.find_by( title: league_key ) ## todo: add synonyms/alt names - why? why not?? else ## assume "verbatim/literal" team_key (use as is 1:1) league = League.find_by( key: league_key ) end ## check if it exists if league.nil? logger.error "league with key >>#{league_key}<< missing" exit 1 end elsif key.downcase == 'season' ## note: allow season, Season, etc. season_key = value.to_s.strip season = Season.find_by( key: season_key ) ## check if it exists if season.nil? logger.error "season with key >>#{season_key}<< missing" exit 1 end elsif key.downcase == 'fixtures' || key.downcase == 'sources' ### todo: check for mulitiple fixtures/sources ?? allow disallow?? why? why not? if value.kind_of?(Array) @fixtures += value else # assume plain (single fixture) string @fixtures << value.to_s end else ## todo: add a source location struct to_s or similar (file, line, col) logger.error "unknown event attrib #{key}; skipping attrib" end end # each key,value # check fixtures - if nothing specified; use basename of config (this) file if @fixtures.empty? ## use basename of config file as default (without extension) sources_default = File.basename( @config, File.extname( @config ) ) logger.debug " add default fixture (assume same basename as config file) e.g. >#{sources_default}<" @fixtures << sources_default end logger.debug "find event - league.id: #{league.id}, season.id: #{season.id}" ## note: for now event MUST exist (read-only access) # keep a "cached" reference for later use @event = Event.find_by!( league_id: league.id, season_id: season.id ) @event ## todo/check: ## return a "simple" hash in new (next) version - why? why not?? ## add fixtures (defaults) if missing end |