Class: Kozeki::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/kozeki/item.rb

Overview

Item represents a built item from Source.

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, data:, meta: nil, build: nil) ⇒ Item

Returns a new instance of Item.



18
19
20
21
22
23
# File 'lib/kozeki/item.rb', line 18

def initialize(id:, data:, meta: nil, build: nil)
  @id = id
  @data = data
  @meta = meta
  @build = build
end

Instance Attribute Details

#buildObject (readonly)

Returns the value of attribute build.



25
26
27
# File 'lib/kozeki/item.rb', line 25

def build
  @build
end

#dataObject (readonly)

Returns the value of attribute data.



25
26
27
# File 'lib/kozeki/item.rb', line 25

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



25
26
27
# File 'lib/kozeki/item.rb', line 25

def id
  @id
end

#metaObject (readonly)

Returns the value of attribute meta.



25
26
27
# File 'lib/kozeki/item.rb', line 25

def meta
  @meta
end

Class Method Details

.load_from_json(json_string) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
# File 'lib/kozeki/item.rb', line 8

def self.load_from_json(json_string)
  j = JSON.parse(json_string, symbolize_names: true)
  raise ParseError, ".kind must be `item`" unless j[:kind] == 'item'
  id = j.fetch(:id)
  data = j.fetch(:data)
  meta = j.fetch(:meta, {})
  build = j.fetch(:kozeki_build, {})
  Item.new(id:, data:, meta:, build:)
end

Instance Method Details

#as_jsonObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kozeki/item.rb', line 27

def as_json
  {
    kind: 'item',
    id: id,
    meta: meta.transform_values do |v|
      case v
      when Time
        v.xmlschema
      else
        v
      end
    end,
    data:,
    kozeki_build: build,
  }
end