Module: TextUtils::ValueHelper

Instance Method Summary collapse

Instance Method Details

#is_state?(value) ⇒ Boolean

note: was is_region? (use new name only)

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
# File 'lib/worlddb/helpers/value_helper.rb', line 47

def is_state?( value )   # note: was is_region? (use new name only)
  # assume state code e.g. TX or N
  #
  # fix: allow three letter states too e.g. BRU (brussels)
  match_result =  value =~ /^[A-Z]{1,2}$/
  # match found if 0,1,2,3 etc or no match if nil
  # note: return bool e.g. false|true  (not 0,1,2,3 etc. and nil)
  match_result != nil
end

#match_city(value) ⇒ Object

NB: might be nil (city not found)



74
75
76
77
78
79
80
81
82
83
# File 'lib/worlddb/helpers/value_helper.rb', line 74

def match_city( value )  # NB: might be nil (city not found)
  if value =~ /^city:/   ## city:
    city_key = value[5..-1]  ## cut off city: prefix
    city = WorldDb::Model::City.find_by_key( city_key )
    yield( city )  # NB: might be nil (city not found)
    true # bingo - match found
  else
    false # no match found
  end
end

#match_country(value) ⇒ Object

todo/check: add to pair of matchers?? e.g. match_country and match_country!

- match_country will use find_by_key and match_country will use find_by_key! - why? why not?


15
16
17
18
19
20
21
22
23
24
# File 'lib/worlddb/helpers/value_helper.rb', line 15

def match_country( value )
  if value =~ /^country:/       # country:
    country_key = value[8..-1]  # cut off country: prefix
    country = WorldDb::Model::Country.find_by_key!( country_key )
    yield( country )
    true # bingo - match found
  else
    false # no match found
  end
end

#match_metro(value) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/worlddb/helpers/value_helper.rb', line 85

def match_metro( value )
  if value =~ /^metro:/   ## metro:
    metro_key = value[6..-1]  ## cut off metro: prefix
    metro = WorldDb::Model::Metro.find_by_key!( metro_key )
    yield( metro )
    true # bingo - match found
  else
    false # no match found
  end
end

#match_metro_flag(value) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/worlddb/helpers/value_helper.rb', line 96

def match_metro_flag( value )
  if value =~ /^metro$/   # metro(politan area)
    yield( true )
    true # bingo - match found
  else
    false # no match found
  end
end

#match_metro_pop(value) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/worlddb/helpers/value_helper.rb', line 105

def match_metro_pop( value )
  if value =~ /^m:/   # m:
    num = value[2..-1].gsub(/[ _]/, '').to_i   # cut off m: prefix; allow space and _ in number
    yield( num )
    true # bingo - match found
  else
    false # no match found
  end
end

#match_state_for_country(value, country_id) ⇒ Object

fix/todo: use match_state_for_country! w/ !!! why? why not?



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/worlddb/helpers/value_helper.rb', line 58

def match_state_for_country( value, country_id )  ## NB: required country_id 
  if value =~ /^state:/   ## state:
    state_key = value[6..-1]  ## cut off state: prefix
    state = WorldDb::Model::State.find_by_key_and_country_id!( state_key, country_id )
    yield( state )
    true  # bingo - match found
  elsif is_state?( value )  ## assume state code e.g. TX or N
    state = WorldDb::Model::State.find_by_key_and_country_id!( value.downcase, country_id )
    yield( state )
    true  # bingo - match found
  else
    false # no match found
  end
end

#match_supra(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/worlddb/helpers/value_helper.rb', line 26

def match_supra( value )
  if value =~ /^supra:/         # supra:
    country_key = value[6..-1]  # cut off supra: prefix
    country = WorldDb::Model::Country.find_by_key!( country_key )
    yield( country )
    true # bingo - match found
  else
    false # no match found
  end
end

#match_supra_flag(value) ⇒ Object

supranational (country)



37
38
39
40
41
42
43
44
# File 'lib/worlddb/helpers/value_helper.rb', line 37

def match_supra_flag( value )  # supranational (country)
  if value =~ /^supra$/   # supra(national)
    yield( true )
    true # bingo - match found
  else
    false # no match found
  end
end