Class: CsvHuman::Tag

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = nil, type = String) ⇒ Tag

Returns a new instance of Tag.



112
113
114
115
116
117
# File 'lib/csvhuman/tag.rb', line 112

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

#attributesObject (readonly)

use attribs or something shorter - why? why not?



109
110
111
# File 'lib/csvhuman/tag.rb', line 109

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



108
109
110
# File 'lib/csvhuman/tag.rb', line 108

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



110
111
112
# File 'lib/csvhuman/tag.rb', line 110

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
# File 'lib/csvhuman/tag.rb', line 60

def self.guess_type( name, attributes )

  if name == 'date'
     Date
  elsif ['affected', 'inneed', 'targeted', 'reached'].include?( name )
     Integer
  else
    ## check attributes
    if attributes.nil? || attributes.empty?
      String  ## assume (default to) string
    elsif attributes.include?( 'num' )
      Integer
    elsif attributes.include?( 'date' )   ### todo/check: exists +date?
      Date
    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



95
96
97
98
99
100
101
102
103
# File 'lib/csvhuman/tag.rb', line 95

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

#keyObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/csvhuman/tag.rb', line 120

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_sObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/csvhuman/tag.rb', line 135

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?



151
152
153
154
155
156
157
158
# File 'lib/csvhuman/tag.rb', line 151

def typecast( value )   ## use convert or call - why? why not?
  if @type == Integer
    conv_to_i( value )
  else   ## assume String
    # pass through as is
    value
  end
end