Class: Factbook::Codes
- Inherits:
-
Object
- Object
- Factbook::Codes
- Defined in:
- lib/factbook/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 ⇒ 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
- #size ⇒ Object
- #south_america ⇒ Object
- #south_asia ⇒ Object
- #to_a ⇒ Object
- #world ⇒ Object
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
#africa ⇒ Object
83 |
# File 'lib/factbook/codes.rb', line 83 def africa() region 'Africa'; end |
#antartica ⇒ Object
88 |
# File 'lib/factbook/codes.rb', line 88 def antartica() region 'Antarctica'; end |
#australia_oceania ⇒ Object
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_caribbean ⇒ Object
85 |
# File 'lib/factbook/codes.rb', line 85 def central_america_n_caribbean() region 'Central America and Caribbean'; end |
#central_asia ⇒ Object
80 |
# File 'lib/factbook/codes.rb', line 80 def central_asia() region 'Central Asia'; end |
#countries ⇒ Object
“pre-defined” convenience shortcuts
69 |
# File 'lib/factbook/codes.rb', line 69 def countries() category 'Countries'; end |
#dependencies ⇒ Object
74 |
# File 'lib/factbook/codes.rb', line 74 def dependencies() category 'Dependencies'; end |
#dependencies_us ⇒ Object
75 |
# File 'lib/factbook/codes.rb', line 75 def dependencies_us() category 'Dependencies (United States)'; end |
#each ⇒ Object
58 59 60 |
# File 'lib/factbook/codes.rb', line 58 def each @codes.each {|code| yield( code ) } end |
#east_n_souteast_asia ⇒ Object
81 |
# File 'lib/factbook/codes.rb', line 81 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.
78 |
# File 'lib/factbook/codes.rb', line 78 def europe() region 'Europe'; end |
#middle_east ⇒ Object
82 |
# File 'lib/factbook/codes.rb', line 82 def middle_east() region 'Middle East'; end |
#misc ⇒ Object
72 |
# File 'lib/factbook/codes.rb', line 72 def misc() category 'Miscellaneous'; end |
#north_america ⇒ Object
84 |
# File 'lib/factbook/codes.rb', line 84 def north_america() region 'North America'; end |
#oceans ⇒ Object
71 |
# File 'lib/factbook/codes.rb', line 71 def oceans() category 'Oceans'; end |
#others ⇒ Object
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 |
#size ⇒ Object
56 |
# File 'lib/factbook/codes.rb', line 56 def size() @codes.size; end |
#south_america ⇒ Object
86 |
# File 'lib/factbook/codes.rb', line 86 def south_america() region 'South America'; end |
#south_asia ⇒ Object
79 |
# File 'lib/factbook/codes.rb', line 79 def south_asia() region 'South Asia'; end |
#to_a ⇒ Object
62 63 64 |
# File 'lib/factbook/codes.rb', line 62 def to_a @codes.collect {|code| code.code } ## return array of codes end |
#world ⇒ Object
70 |
# File 'lib/factbook/codes.rb', line 70 def world() category 'World'; end |