Class: Factbook::Codes

Inherits:
Object
  • Object
show all
Defined in:
lib/factbook/codes.rb

Defined Under Namespace

Classes: Code

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(codes) ⇒ Codes

Returns a new instance of Codes.



52
53
54
# File 'lib/factbook/codes.rb', line 52

def initialize( codes )
  @codes = codes
end

Class Method Details

.from_csv(path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/factbook/codes.rb', line 19

def self.from_csv( path )
  ###

  #  note:

  #   if you use quotes - NO leading spaces allowed e.g.

  #  use au,"Austria",... and NOT

  #      au, "Austria", ...

  #

  #  for headers - NO leading spaces allowed e.g.

  #   use  Code,Name,Category,Region,...   and NOT

  #        Code, Name, Category, Region, ...


  rows = CSV.read( path, headers: true )
  
  pp rows

  recs = []
  rows.each do |row|
    pp row
    rec = Code.new
    rec.code     = row['Code'].strip    ## remove leading n trailing whitespaces

    rec.name     = row['Name'].strip

    ## note: for now category and region are optional

    rec.category = row['Category'].strip    if row['Category']
    rec.region   = row['Region'].strip      if row['Region']  

    pp rec
    recs << rec
  end

  self.new( recs )
end

Instance Method Details

#africaObject



83
# File 'lib/factbook/codes.rb', line 83

def africa()               region 'Africa';            end

#antarticaObject



88
# File 'lib/factbook/codes.rb', line 88

def antartica()            region 'Antarctica';        end

#australia_oceaniaObject



87
# File 'lib/factbook/codes.rb', line 87

def australia_oceania()    region 'Australia-Oceania'; end

#category(query) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/factbook/codes.rb', line 96

def category( query )
  ## todo/future: allow passing in of regex too (not just string)

  ## note: e.g. Dependencies (France) needs to get escpaed to

  ##            Dependencies \(France\)  etc.

  filter_regex = /#{Regexp.escape(query)}/i
  codes = @codes.select do |code|
    code.category ? filter_regex.match( code.category ) : false   ## note: allow nil for category; will fail on search

  end
  Codes.new( codes )   ## return new Codes obj for easy-chaining

end

#central_america_n_caribbeanObject



85
# File 'lib/factbook/codes.rb', line 85

def central_america_n_caribbean() region 'Central America and Caribbean'; end

#central_asiaObject



80
# File 'lib/factbook/codes.rb', line 80

def central_asia()         region 'Central Asia';      end

#countriesObject

“pre-defined” convenience shortcuts



69
# File 'lib/factbook/codes.rb', line 69

def countries()       category 'Countries';     end

#dependenciesObject



74
# File 'lib/factbook/codes.rb', line 74

def dependencies()    category 'Dependencies';  end

#dependencies_usObject



75
# File 'lib/factbook/codes.rb', line 75

def dependencies_us() category 'Dependencies (United States)'; end

#eachObject



58
59
60
# File 'lib/factbook/codes.rb', line 58

def each
  @codes.each {|code| yield( code ) }
end

#east_n_souteast_asiaObject



81
# File 'lib/factbook/codes.rb', line 81

def east_n_souteast_asia() region 'East & Southeast Asia'; end

#europeObject

fix/todo: add all dependencies uk (or gb?), fr,cn,au,nz,no,dk,etc.



78
# File 'lib/factbook/codes.rb', line 78

def europe()               region 'Europe';            end

#middle_eastObject



82
# File 'lib/factbook/codes.rb', line 82

def middle_east()          region 'Middle East';       end

#miscObject



72
# File 'lib/factbook/codes.rb', line 72

def misc()            category 'Miscellaneous'; end

#north_americaObject



84
# File 'lib/factbook/codes.rb', line 84

def north_america()        region 'North America';     end

#oceansObject



71
# File 'lib/factbook/codes.rb', line 71

def oceans()          category 'Oceans';        end

#othersObject



73
# File 'lib/factbook/codes.rb', line 73

def others()          category 'Other';         end

#region(query) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/factbook/codes.rb', line 107

def region( query )
  ## todo/future: allow passing in of regex too (not just string)

  filter_regex = /#{Regexp.escape(query)}/i
  codes = @codes.select do |code|
     code.region ? filter_regex.match( code.region ) : false      ## note: allow nil for region; will fail on search

  end
  Codes.new( codes )   ## return new Codes obj for easy-chaining

end

#sizeObject



56
# File 'lib/factbook/codes.rb', line 56

def size() @codes.size; end

#south_americaObject



86
# File 'lib/factbook/codes.rb', line 86

def south_america()        region 'South America';     end

#south_asiaObject



79
# File 'lib/factbook/codes.rb', line 79

def south_asia()           region 'South Asia';        end

#to_aObject



62
63
64
# File 'lib/factbook/codes.rb', line 62

def to_a
  @codes.collect {|code| code.code }   ## return array of codes

end

#worldObject



70
# File 'lib/factbook/codes.rb', line 70

def world()           category 'World';         end