Class: Factbook::Codes

Inherits:
Object
  • Object
show all
Defined in:
lib/factbook-codes/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.



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

def initialize( codes )
  @codes = codes
end

Class Method Details

.read_csv(path) ⇒ Object



17
18
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
# File 'lib/factbook-codes/codes.rb', line 17

def self.read_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 = CsvHash.read( path )

  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'] && row['Category'].size > 0
    rec.region   = row['Region'].strip     if row['Region'] && row['Region'].size > 0

    pp rec
    recs << rec
  end

  new( recs )
end

Instance Method Details

#africaObject



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

def africa()               region 'Africa';            end

#antarticaObject



90
# File 'lib/factbook-codes/codes.rb', line 90

def antartica()            region 'Antarctica';        end

#australia_oceaniaObject



89
# File 'lib/factbook-codes/codes.rb', line 89

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

#category(query) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/factbook-codes/codes.rb', line 98

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



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

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

#central_asiaObject



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

def central_asia()         region 'Central Asia';      end

#countriesObject

“pre-defined” convenience shortcuts



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

def countries()       category 'Countries';     end

#dependenciesObject



76
# File 'lib/factbook-codes/codes.rb', line 76

def dependencies()    category 'Dependencies';  end

#dependencies_usObject



77
# File 'lib/factbook-codes/codes.rb', line 77

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

#each(&blk) ⇒ Object



57
# File 'lib/factbook-codes/codes.rb', line 57

def each( &blk ) @codes.each( &blk ); end

#east_n_souteast_asiaObject



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

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.



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

def europe()               region 'Europe';            end

#middle_eastObject



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

def middle_east()          region 'Middle East';       end

#miscObject



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

def misc()            category 'Miscellaneous'; end

#north_americaObject



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

def north_america()        region 'North America';     end

#oceansObject



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

def oceans()          category 'Oceans';        end

#othersObject



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

def others()          category 'Other';         end

#region(query) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/factbook-codes/codes.rb', line 109

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

#select(&blk) ⇒ Object



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

def select( &blk )
  codes = @codes.select( &blk )
  Codes.new( codes )   ## return (again) new Codes obj for easy-chaining - why? why not?
end

#sizeObject



55
# File 'lib/factbook-codes/codes.rb', line 55

def size() @codes.size; end

#south_americaObject



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

def south_america()        region 'South America';     end

#south_asiaObject



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

def south_asia()           region 'South Asia';        end

#to_aObject



64
65
66
# File 'lib/factbook-codes/codes.rb', line 64

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

#worldObject



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

def world()           category 'World';         end