Class: Kozeki::Source

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

Overview

Source represents a source text file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, meta:, mtime:, content:, loader:) ⇒ Source

Returns a new instance of Source.

Parameters:

  • path (Array<String>)

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kozeki/source.rb', line 11

def initialize(path:, meta:, mtime:, content:, loader:)
  raise ArgumentError, "path fragment cannot include /" if path.any? { _1.include?('/') }
  @path = path
  @meta = meta
  @mtime = mtime
  @content = content

  @loader = loader

  raise ArgumentError, "path fragment cannot include /" if path.any? { _1.include?('/') }
  raise ArgumentError, "id cannot include /" if id.include?('/')
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



24
25
26
# File 'lib/kozeki/source.rb', line 24

def content
  @content
end

#loaderObject (readonly)

Returns the value of attribute loader.



24
25
26
# File 'lib/kozeki/source.rb', line 24

def loader
  @loader
end

#metaObject

Returns the value of attribute meta.



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

def meta
  @meta
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



24
25
26
# File 'lib/kozeki/source.rb', line 24

def mtime
  @mtime
end

#pathObject (readonly)

Returns the value of attribute path.



24
25
26
# File 'lib/kozeki/source.rb', line 24

def path
  @path
end

Instance Method Details

#build_itemObject



50
51
52
53
54
55
56
57
58
# File 'lib/kozeki/source.rb', line 50

def build_item
  data = loader.build(self)
  Item.new(
    id:,
    data:,
    meta:,
    build: {},
  )
end

#collectionsObject



37
38
39
# File 'lib/kozeki/source.rb', line 37

def collections
  meta[:collections] || []
end

#idObject



27
28
29
30
31
# File 'lib/kozeki/source.rb', line 27

def id
  meta.fetch(:id) do
    "ao_#{Digest::SHA256.hexdigest(path.join('/'))}"
  end.to_s
end

#item_pathObject

Relative file path of built Item.



42
43
44
# File 'lib/kozeki/source.rb', line 42

def item_path
  ['items', "#{id}.json"]
end

#timestampObject



33
34
35
# File 'lib/kozeki/source.rb', line 33

def timestamp
  meta[:timestamp]&.then { Time.xmlschema(_1) }
end

#to_recordObject



46
47
48
# File 'lib/kozeki/source.rb', line 46

def to_record
  Record.from_source(self)
end