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



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

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)


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

def city
  @city
end

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



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

def country
  @country
end

#nameString? (readonly)

Returns:

  • (String, nil)


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

def name
  @name
end

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



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

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)


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

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)


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

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)


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

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