Class: Hdo::StortingImporter::Category

Inherits:
Object
  • Object
show all
Includes:
HasJsonSchema, Inspectable, IvarEquality
Defined in:
lib/hdo/storting_importer/category.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasJsonSchema

#as_json, included, schemas, #to_json, #valid?, #validate!

Methods included from Inspectable

#short_inspect_string

Methods included from IvarEquality

#==, #__ivars__, #hash

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

#childrenObject

Returns the value of attribute children.



9
10
11
# File 'lib/hdo/storting_importer/category.rb', line 9

def children
  @children
end

#external_idObject (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

#nameObject (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

.exampleObject



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>)

Returns:



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>)

Returns:



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_inspectObject



60
61
62
# File 'lib/hdo/storting_importer/category.rb', line 60

def short_inspect
  short_inspect_string :include => [:external_id, :name]
end

#to_hashObject



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