Class: Hdo::StortingImporter::Category
- Inherits:
-
Object
- Object
- Hdo::StortingImporter::Category
- Includes:
- HasJsonSchema, Inspectable, IvarEquality
- Defined in:
- lib/hdo/storting_importer/category.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#external_id ⇒ Object
readonly
Returns the value of attribute external_id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .example ⇒ Object
- .from_hash(data) ⇒ Object
-
.from_storting_doc(doc) ⇒ Array<Category>
Deserialize from a Storting XML document (<emne_liste><emne>…</emne></emne_liste>).
-
.from_storting_node(node) ⇒ Category
Deserialize form a Storting XML node (<emne>…</emne>).
Instance Method Summary collapse
-
#initialize(external_id, name) ⇒ Category
constructor
A new instance of Category.
- #short_inspect ⇒ Object
- #to_hash ⇒ Object
Methods included from HasJsonSchema
#as_json, included, schemas, #to_json, #valid?, #validate!
Methods included from Inspectable
Methods included from IvarEquality
Constructor Details
#initialize(external_id, name) ⇒ Category
Returns a new instance of Category.
54 55 56 57 58 |
# File 'lib/hdo/storting_importer/category.rb', line 54 def initialize(external_id, name) @external_id = external_id @name = name @children = [] end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
9 10 11 |
# File 'lib/hdo/storting_importer/category.rb', line 9 def children @children end |
#external_id ⇒ Object (readonly)
Returns the value of attribute external_id.
8 9 10 |
# File 'lib/hdo/storting_importer/category.rb', line 8 def external_id @external_id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/hdo/storting_importer/category.rb', line 8 def name @name end |
Class Method Details
.example ⇒ Object
13 14 15 16 17 18 |
# File 'lib/hdo/storting_importer/category.rb', line 13 def self.example cat = new("5", "Employment") cat.children << new("6", "Wages") cat end |
.from_hash(data) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/hdo/storting_importer/category.rb', line 47 def self.from_hash(data) obj = new data['externalId'], data['name'] obj.children = Array(data['subCategories']).map { |e| from_hash(e) } obj end |
.from_storting_doc(doc) ⇒ Array<Category>
Deserialize from a Storting XML document (<emne_liste><emne>…</emne></emne_liste>)
26 27 28 |
# File 'lib/hdo/storting_importer/category.rb', line 26 def self.from_storting_doc(doc) doc.css("emne_liste > emne").map { |xt| from_storting_node(xt) } end |
.from_storting_node(node) ⇒ Category
Deserialize form a Storting XML node (<emne>…</emne>)
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/hdo/storting_importer/category.rb', line 36 def self.from_storting_node(node) cat = Category.new node.css("id").first.text, node.css("navn").first.text subnodes = node.css("underemne_liste > emne") subnodes.map do |subnode| cat.children << Category.new(subnode.css("id").first.text, subnode.css("navn").first.text) end cat end |
Instance Method Details
#short_inspect ⇒ Object
60 61 62 |
# File 'lib/hdo/storting_importer/category.rb', line 60 def short_inspect short_inspect_string :include => [:external_id, :name] end |
#to_hash ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hdo/storting_importer/category.rb', line 64 def to_hash h = { 'kind' => self.class.kind, 'externalId' => @external_id, 'name' => @name } h['subCategories'] = @children.map { |e| e.to_hash } if @children.any? h end |