Class: RelatonBib::Address

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

Overview

Address class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Address

Returns a new instance of Address.

Parameters:

  • street (Array<String>)

    streets

  • city (String, nil)

    city, should be present or formatted address provided

  • state (String, nil)

    state

  • country (String, nil)

    country, should be present or formatted address provided

  • postcode (String, nil)

    postcode

  • formatted_address (String, nil)

    formatted address, should be present or city and country provided



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/relaton_bib/contributor.rb', line 20

def initialize(**args) # rubocop:disable Metrics/CyclomaticComplexity
  unless args[:formatted_address] || (args[:city] && args[:country])
    raise ArgumentError, "Either formatted address or city and country must be provided"
  end

  @street   = args[:street] || []
  @city     = args[:city]
  @state    = args[:state]
  @country  = args[:country]
  @postcode = args[:postcode]
  @formatted_address = args[:formatted_address] unless args[:city] && args[:country]
end

Instance Attribute Details

#cityString? (readonly)

Returns:

  • (String, nil)


12
13
14
# File 'lib/relaton_bib/contributor.rb', line 12

def city
  @city
end

#countryString? (readonly)

Returns:

  • (String, nil)


12
13
14
# File 'lib/relaton_bib/contributor.rb', line 12

def country
  @country
end

#formatted_addressString? (readonly)

Returns:

  • (String, nil)


12
13
14
# File 'lib/relaton_bib/contributor.rb', line 12

def formatted_address
  @formatted_address
end

#postcodeString? (readonly)

Returns:

  • (String, nil)


12
13
14
# File 'lib/relaton_bib/contributor.rb', line 12

def postcode
  @postcode
end

#stateString? (readonly)

Returns:

  • (String, nil)


12
13
14
# File 'lib/relaton_bib/contributor.rb', line 12

def state
  @state
end

#streetArray<String> (readonly)

Returns:



9
10
11
# File 'lib/relaton_bib/contributor.rb', line 9

def street
  @street
end

Instance Method Details

#to_asciibib(prefix = "", count = 1) ⇒ String

Parameters:

  • prefix (String) (defaults to: "")
  • count (Integer) (defaults to: 1)

    number of addresses

Returns:

  • (String)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/relaton_bib/contributor.rb', line 66

def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  pref = prefix.empty? ? "address" : "#{prefix}.address"
  if formatted_address
    "#{pref}.formatted_address:: #{formatted_address}\n"
  else
    out = count > 1 ? "#{pref}::\n" : ""
    street.each { |st| out += "#{pref}.street:: #{st}\n" }
    out += "#{pref}.city:: #{city}\n"
    out += "#{pref}.state:: #{state}\n" if state
    out += "#{pref}.country:: #{country}\n"
    out += "#{pref}.postcode:: #{postcode}\n" if postcode
    out
  end
end

#to_hashHash

Returns:

  • (Hash)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/relaton_bib/contributor.rb', line 49

def to_hash # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  hash = { "address" => {} }
  if formatted_address
    hash["address"]["formatted_address"] = formatted_address
  else
    hash["address"]["street"] = street if street.any?
    hash["address"]["city"] = city
    hash["address"]["state"] = state if state
    hash["address"]["country"] = country
    hash["address"]["postcode"] = postcode if postcode
  end
  hash
end

#to_xml(doc) ⇒ Object

Parameters:

  • doc (Nokogiri::XML::Document)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/relaton_bib/contributor.rb', line 34

def to_xml(doc) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  doc.address do
    if formatted_address
      doc.formattedAddress formatted_address
    else
      street.each { |str| doc.street str }
      doc.city city
      doc.state state if state
      doc.country country
      doc.postcode postcode if postcode
    end
  end
end