Class: CsvHuman::Tag
- Inherits:
-
Object
- Object
- CsvHuman::Tag
- Defined in:
- lib/csvhuman/tag.rb
Constant Summary collapse
- SEP_REGEX =
1) plus (with optional hashtag and/or optional leading and trailing spaces)
2) hashtag (with optional leading and trailing spaces) 3) spaces only (not followed by plus) or note: plus pattern must go first (otherwise "sector + en" becomes ["sector", "", "en"]) /(?: \s*\++ (?:\s*\#+)? \s* ) | (?: \s*\#+\s* ) | (?: \s+) /x
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
use attribs or something shorter - why? why not?.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .guess_type(name, attributes) ⇒ Object
-
.normalize(value) ⇒ Object
todo: rename to pretty or something or add alias.
- .parse(value) ⇒ Object
- .split(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(name, attributes = nil, type = String) ⇒ Tag
constructor
A new instance of Tag.
- #key ⇒ Object
- #to_s ⇒ Object
-
#typecast(value) ⇒ Object
use convert or call - why? why not?.
Constructor Details
#initialize(name, attributes = nil, type = String) ⇒ Tag
Returns a new instance of Tag.
118 119 120 121 122 123 |
# File 'lib/csvhuman/tag.rb', line 118 def initialize( name, attributes=nil, type=String ) @name = name ## sorted a-z - note: make sure attributes is [] NOT nil if empty - why? why not? @attributes = attributes || [] @type = type ## type class (defaults to String) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
use attribs or something shorter - why? why not?
115 116 117 |
# File 'lib/csvhuman/tag.rb', line 115 def attributes @attributes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
114 115 116 |
# File 'lib/csvhuman/tag.rb', line 114 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
116 117 118 |
# File 'lib/csvhuman/tag.rb', line 116 def type @type end |
Class Method Details
.guess_type(name, attributes) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/csvhuman/tag.rb', line 60 def self.guess_type( name, attributes ) if name == 'date' Date ## todo/fix: add more well-known names with num required!!! elsif ['affected', 'inneed', 'targeted', 'reached', 'population'].include?( name ) Integer else ## check attributes if attributes.nil? || attributes.empty? String ## assume (default to) string elsif attributes.include?( 'num' ) || attributes.include?( 'id') ## assume id is (always) a rowid - why? why not? Integer elsif attributes.include?( 'date' ) ### todo/check: exists +date? Date elsif name == 'geo' && (attributes.include?('lat') || attributes.include?('lon') || attributes.include?('elevation')) Float elsif attributes.include?( 'killed' ) || attributes.include?( 'injured' ) || attributes.include?( 'infected' ) || attributes.include?( 'displaced' ) || attributes.include?( 'idps' ) || attributes.include?( 'refugees' ) || attributes.include?( 'abducted' ) || attributes.include?( 'threatened' ) || attributes.include?( 'affected' ) || attributes.include?( 'inneed' ) || attributes.include?( 'targeted' ) || attributes.include?( 'reached' ) Integer else String ## assume (default to) string end end end |
.normalize(value) ⇒ Object
todo: rename to pretty or something or add alias
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/csvhuman/tag.rb', line 43 def self.normalize( value ) ## todo: rename to pretty or something or add alias parts = split( value ) name = parts[0] attributes = parts[1..-1] ## note: might be nil buf = '' if name ## note: name might be nil too e.g. value = "" or value = " " buf << '#' + name if attributes && attributes.size > 0 buf << ' +' buf << attributes.join(' +') end end buf end |
.parse(value) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/csvhuman/tag.rb', line 101 def self.parse( value ) parts = split( value ) name = parts[0] attributes = parts[1..-1] ## todo/fix: check if nil (make it empty array [] always) - why? why not? type = guess_type( name, attributes ) new( name, attributes, type ) end |
.split(value) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/csvhuman/tag.rb', line 24 def self.split( value ) value = value.strip value = value.downcase while value.start_with?('#') do ## allow one or more hashes value = value[1..-1] ## remove leading # value = value.strip ## strip (optional) leading spaces (again) end ## pp value parts = value.split( SEP_REGEX ) ## sort attributes a-z if parts.size > 2 [parts[0]] + parts[1..-1].sort else parts end end |
Instance Method Details
#key ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/csvhuman/tag.rb', line 126 def key ## convenience short cut for "standard/default" string key ## cache/pre-built/memoize - why? why not? ## builds: ## population+affected+children+f buf = '' buf << @name if @attributes && @attributes.size > 0 buf << '+' buf << @attributes.join('+') end buf end |
#to_s ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/csvhuman/tag.rb', line 141 def to_s ## cache/pre-built/memoize - why? why not? ## ## builds ## #population +affected +children +f buf = '' buf << '#' + @name if @attributes && @attributes.size > 0 buf << ' +' buf << @attributes.join(' +') end buf end |
#typecast(value) ⇒ Object
use convert or call - why? why not?
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/csvhuman/tag.rb', line 157 def typecast( value ) ## use convert or call - why? why not? if @type == Integer conv_to_i( value ) elsif @type == Float conv_to_f( value ) elsif @type == Date conv_to_date( value ) else ## assume String # pass through as is value end end |