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.



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

def initialize( codes )
  @codes = codes
end

Class Method Details

.read_csv(path) ⇒ Object



37
38
39
40
41
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
# File 'lib/factbook-codes/codes.rb', line 37

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
    rec.category = row['Category'].strip
    rec.region   = row['Region'].strip

    ## pp rec
    recs << rec
  end

  new( recs )
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key) @codes[ key ]; end

#africaObject



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

def africa()               region 'Africa';            end

#antarticaObject



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

def antartica()            region 'Antarctica';        end

#australia_oceaniaObject



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

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

#category(query) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/factbook-codes/codes.rb', line 118

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



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

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

#central_asiaObject



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

def central_asia()         region 'Central Asia';      end

#countriesObject

“pre-defined” convenience shortcuts



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

def countries()       category 'Countries';     end

#dependenciesObject



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

def dependencies()    category 'Dependencies';  end

#dependencies_usObject



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

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

#each(&blk) ⇒ Object



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

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

#east_n_souteast_asiaObject



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

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.



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

def europe()               region 'Europe';            end

#middle_eastObject



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

def middle_east()          region 'Middle East';       end

#miscObject



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

def misc()            category 'Miscellaneous'; end

#north_americaObject



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

def north_america()        region 'North America';     end

#oceansObject



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

def oceans()          category 'Oceans';        end

#othersObject



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

def others()          category 'Other';         end

#region(query) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/factbook-codes/codes.rb', line 129

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



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

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

#sizeObject



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

def size() @codes.size; end

#south_americaObject



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

def south_america()        region 'South America';     end

#south_asiaObject



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

def south_asia()           region 'South Asia';        end

#to_aObject



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

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

#worldObject



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

def world()           category 'World';         end