Class: SportDb::SeasonReader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, Model
Defined in:
lib/sportdb/readers/season.rb

Constant Summary

Constants included from Model

Model::City, Model::Continent, Model::Country, Model::Person, Model::Prop, Model::Region

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ SeasonReader

Returns a new instance of SeasonReader.



37
38
39
40
# File 'lib/sportdb/readers/season.rb', line 37

def initialize( text )
  ## todo/fix: how to add opts={} ???
  @text = text
end

Class Method Details

.from_file(path) ⇒ Object



25
26
27
28
29
30
# File 'lib/sportdb/readers/season.rb', line 25

def self.from_file( path )
  ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
  ## - see textutils/utils.rb
  text = File.read_utf8( path )
  self.from_string( text )
end

.from_string(text) ⇒ Object



32
33
34
# File 'lib/sportdb/readers/season.rb', line 32

def self.from_string( text )
  SeasonReader.new( text )
end

.from_zip(zip_file, entry_path) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/sportdb/readers/season.rb', line 15

def self.from_zip( zip_file, entry_path )
  ## 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 )

  self.from_string( text )
end

Instance Method Details

#readObject



42
43
44
45
46
47
48
49
50
51
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
# File 'lib/sportdb/readers/season.rb', line 42

def read()
  reader = LineReader.from_string( @text )

####
## fix!!!!!
##   use Season.create_or_update_from_hash or similar
##   use Season.create_or_update_from_hash_reader?? or similar
#   move parsing code to model

  reader.each_line do |line|

    # for now assume single value
    logger.debug ">#{line}<"

    key = line

    logger.debug "  find season key: #{key}"
    season = Season.find_by_key( key )

    season_attribs = {}

    ## check if it exists
    if season.present?
      logger.debug "update season #{season.id}-#{season.key}:"
    else
      logger.debug "create season:"
      season = Season.new
      season_attribs[ :key ] = key
    end

    season_attribs[ :title ] = key # for now key n title are the same
   
    logger.debug season_attribs.to_json
        
    season.update_attributes!( season_attribs )
  end # each line

end