Class: Factbook::Codes
- Inherits:
-
Object
- Object
- Factbook::Codes
- Defined in:
- lib/factbook-codes/codes.rb
Defined Under Namespace
Classes: Code
Class Method Summary collapse
Instance Method Summary collapse
- #africa ⇒ Object
- #antartica ⇒ Object
- #australia_oceania ⇒ Object
- #category(query) ⇒ Object
- #central_america_n_caribbean ⇒ Object
- #central_asia ⇒ Object
-
#countries ⇒ Object
“pre-defined” convenience shortcuts.
- #dependencies ⇒ Object
- #dependencies_us ⇒ Object
- #each(&blk) ⇒ Object
- #east_n_souteast_asia ⇒ Object
-
#europe ⇒ Object
fix/todo: add all dependencies uk (or gb?), fr,cn,au,nz,no,dk,etc.
-
#initialize(codes) ⇒ Codes
constructor
A new instance of Codes.
- #middle_east ⇒ Object
- #misc ⇒ Object
- #north_america ⇒ Object
- #oceans ⇒ Object
- #others ⇒ Object
- #region(query) ⇒ Object
- #select(&blk) ⇒ Object
- #size ⇒ Object
- #south_america ⇒ Object
- #south_asia ⇒ Object
- #to_a ⇒ Object
- #world ⇒ Object
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
#africa ⇒ Object
85 |
# File 'lib/factbook-codes/codes.rb', line 85 def africa() region 'Africa'; end |
#antartica ⇒ Object
90 |
# File 'lib/factbook-codes/codes.rb', line 90 def antartica() region 'Antarctica'; end |
#australia_oceania ⇒ Object
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_caribbean ⇒ Object
87 |
# File 'lib/factbook-codes/codes.rb', line 87 def central_america_n_caribbean() region 'Central America and Caribbean'; end |
#central_asia ⇒ Object
82 |
# File 'lib/factbook-codes/codes.rb', line 82 def central_asia() region 'Central Asia'; end |
#countries ⇒ Object
“pre-defined” convenience shortcuts
71 |
# File 'lib/factbook-codes/codes.rb', line 71 def countries() category 'Countries'; end |
#dependencies ⇒ Object
76 |
# File 'lib/factbook-codes/codes.rb', line 76 def dependencies() category 'Dependencies'; end |
#dependencies_us ⇒ Object
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_asia ⇒ Object
83 |
# File 'lib/factbook-codes/codes.rb', line 83 def east_n_souteast_asia() region 'East & Southeast Asia'; end |
#europe ⇒ Object
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_east ⇒ Object
84 |
# File 'lib/factbook-codes/codes.rb', line 84 def middle_east() region 'Middle East'; end |
#misc ⇒ Object
74 |
# File 'lib/factbook-codes/codes.rb', line 74 def misc() category 'Miscellaneous'; end |
#north_america ⇒ Object
86 |
# File 'lib/factbook-codes/codes.rb', line 86 def north_america() region 'North America'; end |
#oceans ⇒ Object
73 |
# File 'lib/factbook-codes/codes.rb', line 73 def oceans() category 'Oceans'; end |
#others ⇒ Object
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 |
#size ⇒ Object
55 |
# File 'lib/factbook-codes/codes.rb', line 55 def size() @codes.size; end |
#south_america ⇒ Object
88 |
# File 'lib/factbook-codes/codes.rb', line 88 def south_america() region 'South America'; end |
#south_asia ⇒ Object
81 |
# File 'lib/factbook-codes/codes.rb', line 81 def south_asia() region 'South Asia'; end |
#to_a ⇒ Object
64 65 66 |
# File 'lib/factbook-codes/codes.rb', line 64 def to_a @codes.collect {|code| code.code } ## return array of codes end |
#world ⇒ Object
72 |
# File 'lib/factbook-codes/codes.rb', line 72 def world() category 'World'; end |