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.
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
#city ⇒ String? (readonly)
5 6 7 |
# File 'lib/relaton_bib/place.rb', line 5 def city @city end |
#country ⇒ Array<RelatonBib::Place::RegionType> (readonly)
8 9 10 |
# File 'lib/relaton_bib/place.rb', line 8 def country @country end |
#name ⇒ String? (readonly)
5 6 7 |
# File 'lib/relaton_bib/place.rb', line 5 def name @name end |
#region ⇒ Array<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.
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_hash ⇒ Hash
Render place as 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.
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 |