Class: RelatonBib::Place

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

Defined Under Namespace

Classes: RegionType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, city: nil, region: [], country: []) ⇒ Place

Initialize place.

Parameters:

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

    name of place, name or city should be provided

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

    name of city, city or name should be provided

  • region (Array<RelatonBib::Place::RegionType>) (defaults to: [])

    region of place

  • country (Array<RelatonBib::Place::RegionType>) (defaults to: [])

    country of place



17
18
19
20
21
22
23
24
25
26
# File 'lib/relaton_bib/place.rb', line 17

def initialize(name: nil, city: nil, region: [], country: []) # rubocop:disable Metrics/CyclomaticComplexity
  if name.nil? && city.nil?
    raise ArgumentError, "`name` or `city` should be provided"
  end

  @name    = name
  @city    = city
  @region  = region.map { |r| r.is_a?(Hash) ? RegionType.new(**r) : r }
  @country = country.map { |c| c.is_a?(Hash) ? RegionType.new(**c) : c }
end

Instance Attribute Details

#cityString? (readonly)

Returns:

  • (String, nil)


4
5
6
# File 'lib/relaton_bib/place.rb', line 4

def city
  @city
end

#countryArray<RelatonBib::Place::RegionType> (readonly)



7
8
9
# File 'lib/relaton_bib/place.rb', line 7

def country
  @country
end

#nameString? (readonly)

Returns:

  • (String, nil)


4
5
6
# File 'lib/relaton_bib/place.rb', line 4

def name
  @name
end

#regionArray<RelatonBib::Place::RegionType> (readonly)



7
8
9
# File 'lib/relaton_bib/place.rb', line 7

def region
  @region
end

Instance Method Details

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

Render place as AsciiBib.

Parameters:

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

    number of places

Returns:

  • (Stirng)


68
69
70
71
72
73
74
75
76
# File 'lib/relaton_bib/place.rb', line 68

def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize
  pref = prefix.empty? ? "place" : "#{prefix}.place"
  out = count > 1 ? "#{pref}::\n" : ""
  return "#{out}#{pref}.name:: #{name}\n" if name

  out += "#{pref}.city:: #{city}\n"
  out += region.map { |r| r.to_asciibib("#{pref}.region", region.size) }.join
  out + country.map { |c| c.to_asciibib("#{pref}.country", country.size) }.join
end

#to_hashHash

Render place as Hash.

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
58
# File 'lib/relaton_bib/place.rb', line 50

def to_hash
  if name then name
  else
    hash = { "city" => city }
    hash["region"] = region.map(&:to_hash) if region.any?
    hash["country"] = country.map(&:to_hash) if country.any?
    hash
  end
end

#to_xml(builder) ⇒ Object

Render place as XML.

Parameters:

  • builder (Nologiri::XML::Builder)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/relaton_bib/place.rb', line 33

def to_xml(builder)
  if name
    builder.place name
  else
    builder.place do |b|
      b.city city
      region.each { |r| b.region { r.to_xml b } }
      country.each { |c| b.country { c.to_xml b } }
    end
  end
end