Class: SportDb::EventReader
- Inherits:
-
Object
- Object
- SportDb::EventReader
- Includes:
- LogUtils::Logging, Model
- Defined in:
- lib/sportdb/readers/event.rb
Constant Summary
Constants included from Model
Model::City, Model::Continent, Model::Country, Model::Prop, Model::Region
Instance Attribute Summary collapse
-
#event ⇒ Object
readonly
returns event record; call read first.
-
#include_path ⇒ Object
readonly
Returns the value of attribute include_path.
Instance Method Summary collapse
- #fixtures ⇒ Object
-
#initialize(include_path, opts = {}) ⇒ EventReader
constructor
A new instance of EventReader.
- #read(name, more_attribs = {}) ⇒ Object
Constructor Details
#initialize(include_path, opts = {}) ⇒ EventReader
Returns a new instance of EventReader.
18 19 20 21 22 23 24 |
# File 'lib/sportdb/readers/event.rb', line 18 def initialize( include_path, opts = {} ) @include_path = include_path @name = nil @event = nil @fixtures = [] end |
Instance Attribute Details
#event ⇒ Object (readonly)
returns event record; call read first
16 17 18 |
# File 'lib/sportdb/readers/event.rb', line 16 def event @event end |
#include_path ⇒ Object (readonly)
Returns the value of attribute include_path.
15 16 17 |
# File 'lib/sportdb/readers/event.rb', line 15 def include_path @include_path end |
Instance Method Details
#fixtures ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sportdb/readers/event.rb', line 27 def fixtures ## note: needs to call read first (to set @name, @fixtures, etc.) if @fixtures.empty? ## logger.warn "no fixtures found for event - >#{name}<; assume fixture name is the same as event" fixtures_with_path = [ @name ] else ## add path to fixtures (use path from event e.g) # - bl + at-austria!/2012_13/bl -> at-austria!/2012_13/bl # - bl_ii + at-austria!/2012_13/bl -> at-austria!/2012_13/bl_ii dir = File.dirname( @name ) # use dir for fixtures fixtures_with_path = @fixtures.map do |fx| fx_new = "#{dir}/#{fx}" # add path upfront logger.debug "fx: #{fx_new} | >#{fx}< + >#{dir}<" fx_new end end fixtures_with_path end |
#read(name, more_attribs = {}) ⇒ Object
52 53 54 55 56 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/sportdb/readers/event.rb', line 52 def read( name, more_attribs={} ) @fixtures = [] # reset cached fixtures @event = nil # reset cached event rec @name = name # keep name (needed for fixtures attrib getter) #### ## fix!!!!! ## use Event.create_or_update_from_hash or similar ## use Event.create_or_update_from_hash_reader?? or similar # move parsing code to model reader = HashReaderV2.new( name, include_path ) event_attribs = {} ## set default sources to basename by convention # e.g 2013_14/bl => bl # etc. # use fixtures/sources: to override default event_attribs[ 'sources' ] = File.basename( name ) event_attribs[ 'config' ] = File.basename( name ) # name a of .yml file reader.each_typed do |key, value| ## puts "processing event attrib >>#{key}<< >>#{value}<<..." if key == 'league' league = League.find_by_key( value.to_s.strip ) ## check if it exists if league.present? event_attribs['league_id'] = league.id else logger.error "league with key >>#{value.to_s.strip}<< missing" exit 1 end elsif key == 'season' season = Season.find_by_key( value.to_s.strip ) ## check if it exists if season.present? event_attribs['season_id'] = season.id else logger.error "season with key >>#{value.to_s.strip}<< missing" exit 1 end elsif key == 'start_at' || key == 'begin_at' if value.is_a?(DateTime) || value.is_a?(Date) start_at = value else # assume it's a string start_at = DateTime.strptime( value.to_s.strip, '%Y-%m-%d' ) end event_attribs['start_at'] = start_at elsif key == 'end_at' || key == 'stop_at' if value.is_a?(DateTime) || value.is_a?(Date) end_at = value else # assume it's a string end_at = DateTime.strptime( value.to_s.strip, '%Y-%m-%d' ) end event_attribs['end_at'] = end_at elsif key == 'grounds' || key == 'stadiums' || key == 'venues' ## assume grounds value is an array ## ## note: for now we allow invalid ground keys ## will skip keys not found ground_ids = [] value.each do |item| ground_key = item.to_s.strip ground = Ground.find_by_key( ground_key ) if ground.nil? puts "[warn] ground/stadium w/ key >#{ground_key}< not found; skipping ground" else ground_ids << ground.id end end event_attribs['ground_ids'] = ground_ids elsif key == 'teams' ## assume teams value is an array team_ids = [] value.each do |item| team_key = item.to_s.strip team = Team.find_by_key!( team_key ) team_ids << team.id end event_attribs['team_ids'] = team_ids elsif key == 'team3' ## for now always assume false # todo: fix - use value and convert to boolean if not boolean event_attribs['team3'] = false elsif key == 'fixtures' || key == 'sources' ### todo: check for mulitiple fixtures/sources ?? allow disallow?? why? why not? if value.kind_of?(Array) event_attribs['sources'] = value.join(',') @fixtures += value else # assume plain (single fixture) string event_attribs['sources'] = value.to_s @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 league_id = event_attribs['league_id'] season_id = event_attribs['season_id'] logger.debug "find event - league_id: #{league_id}, season_id: #{season_id}" event = Event.find_by_league_id_and_season_id( league_id, season_id ) ## check if it exists if event.present? logger.debug "*** update event #{event.id}-#{event.key}:" else logger.debug "*** create event:" event = Event.new end logger.debug event_attribs.to_json event.update_attributes!( event_attribs ) # keep a cached reference for later use @event = event end |