Class: RelatonBib::Place
Defined Under Namespace
Classes: RegionType
Instance Attribute Summary collapse
- #city ⇒ String? readonly
- #country ⇒ Array<RelatonBib::Place::RegionType> readonly
- #name ⇒ String? readonly
- #region ⇒ Array<RelatonBib::Place::RegionType> readonly
Instance Method Summary collapse
-
#initialize(name: nil, city: nil, region: [], country: []) ⇒ Place
constructor
Initialize place.
-
#to_asciibib(prefix = "", count = 1) ⇒ Stirng
Render place as AsciiBib.
-
#to_hash ⇒ Hash
Render place as Hash.
-
#to_xml(builder) ⇒ Object
Render place as XML.
Constructor Details
#initialize(name: nil, city: nil, region: [], country: []) ⇒ Place
Initialize 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
#city ⇒ String? (readonly)
4 5 6 |
# File 'lib/relaton_bib/place.rb', line 4 def city @city end |
#country ⇒ Array<RelatonBib::Place::RegionType> (readonly)
7 8 9 |
# File 'lib/relaton_bib/place.rb', line 7 def country @country end |
#name ⇒ String? (readonly)
4 5 6 |
# File 'lib/relaton_bib/place.rb', line 4 def name @name end |
#region ⇒ Array<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.
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_hash ⇒ Hash
Render place as 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.
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 |