Class: RelatonBib::Place::RegionType

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_bib/place.rb

Constant Summary collapse

STATES =
{
  "AK" =>	"Alaska",
  "AL" =>	"Alabama",
  "AR" =>	"Arkansas",
  "AZ" =>	"Arizona",
  "CA" =>	"California",
  "CO" =>	"Colorado",
  "CT" =>	"Connecticut",
  "DC" =>	"District Of Columbia",
  "DE" =>	"Delaware",
  "FL" =>	"Florida",
  "GA" =>	"Georgia",
  "GU" =>	"Guam",
  "HI" =>	"Hawaii",
  "IA" =>	"Iowa",
  "ID" =>	"Idaho",
  "IL" =>	"Illinois",
  "IN" =>	"Indiana",
  "KS" =>	"Kansas",
  "KY" =>	"Kentucky",
  "LA" =>	"Louisiana",
  "MA" =>	"Massachusetts",
  "MD" =>	"Maryland",
  "ME" =>	"Maine",
  "MI" =>	"Michigan",
  "MN" =>	"Minnesota",
  "MO" =>	"Missouri",
  "MS" =>	"Mississippi",
  "MT" =>	"Montana",
  "NC" =>	"North Carolina",
  "ND" =>	"North Dakota",
  "NE" =>	"Nebraska",
  "NH" =>	"New Hampshire",
  "NJ" =>	"New Jersey",
  "NM" =>	"New Mexico",
  "NV" =>	"Nevada",
  "NY" =>	"New York",
  "OH" =>	"Ohio",
  "OK" =>	"Oklahoma",
  "OR" =>	"Oregon",
  "PA" =>	"Pennsylvania",
  "PR" =>	"Puerto Rico",
  "RI" =>	"Rhode Island",
  "SC" =>	"South Carolina",
  "SD" =>	"South Dakota",
  "TN" =>	"Tennessee",
  "TX" =>	"Texas",
  "UT" =>	"Utah",
  "VA" =>	"Virginia",
  "VI" =>	"Virgin Islands",
  "VT" =>	"Vermont",
  "WA" =>	"Washington",
  "WI" =>	"Wisconsin",
  "WV" =>	"West Virginia",
  "WY" =>	"Wyoming",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, iso: nil, recommended: nil) ⇒ RegionType

Initialize region type. Name or valid US state ISO code should be provided.

Parameters:

  • name (String, nil) (defaults to: nil)

    name of region

  • iso (String, nil) (defaults to: nil)

    ISO code of region

  • recommended (Boolean, nil) (defaults to: nil)

    recommended region



153
154
155
156
157
158
159
160
161
# File 'lib/relaton_bib/place.rb', line 153

def initialize(name: nil, iso: nil, recommended: nil)
  unless name || STATES.key?(iso&.upcase)
    raise ArgumentError, "`name` or valid US state ISO code should be provided"
  end

  @name = name || STATES[iso&.upcase]
  @iso  = iso
  @recommended = recommended
end

Instance Attribute Details

#isoStrign? (readonly)

Returns ISO code of region.

Returns:

  • (Strign, nil)

    ISO code of region



141
142
143
# File 'lib/relaton_bib/place.rb', line 141

def iso
  @iso
end

#nameStrign (readonly)

Returns name of region.

Returns:

  • (Strign)

    name of region



138
139
140
# File 'lib/relaton_bib/place.rb', line 138

def name
  @name
end

Returns:

  • (Boolean, nil)


144
145
146
# File 'lib/relaton_bib/place.rb', line 144

def recommended
  @recommended
end

Instance Method Details

#to_asciibib(pref, count = 1) ⇒ String

Render region type as AsciiBib.

Parameters:

  • pref (String)

    prefix

  • count (Integer) (defaults to: 1)

    number of region types

Returns:

  • (String)

    region type as AsciiBib



194
195
196
197
198
199
200
# File 'lib/relaton_bib/place.rb', line 194

def to_asciibib(pref, count = 1) # rubocop:disable Metrics/AbcSize
  out = count > 1 ? "#{pref}::\n" : ""
  out += "#{pref}.name:: #{name}\n"
  out += "#{pref}.iso:: #{iso}\n" if iso
  out += "#{pref}.recommended:: #{recommended}\n" if recommended
  out
end

#to_hashHash

Render region type as Hash.

Returns:

  • (Hash)

    region type as Hash



179
180
181
182
183
184
# File 'lib/relaton_bib/place.rb', line 179

def to_hash
  hash = { "name" => name }
  hash["iso"] = iso if iso
  hash["recommended"] = recommended unless recommended.nil?
  hash
end

#to_xml(builder) ⇒ Object

Render region type as XML.

Parameters:

  • builder (Nokogiri::XML::Builder)

    XML builder



168
169
170
171
172
# File 'lib/relaton_bib/place.rb', line 168

def to_xml(builder)
  builder.parent["iso"] = iso if iso
  builder.parent["recommended"] = recommended.to_s unless recommended.nil?
  builder.text name
end