Class: Paneron::Register::Raw::ItemClass

Inherits:
Object
  • Object
show all
Defined in:
lib/paneron/register/raw/item_class.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_set_path, item_class_name, extension = "yaml") ⇒ ItemClass

Returns a new instance of ItemClass.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/paneron/register/raw/item_class.rb', line 12

def initialize(
  data_set_path,
  item_class_name,
  extension = "yaml"
)
  item_class_path = File.join(data_set_path, item_class_name)
  self.class.validate_item_class_path(item_class_path)
  @extension = extension
  @data_set_path = data_set_path
  @item_class_name = item_class_name
  @item_class_path = item_class_path
  @items_uuids = nil
  @items = {}
end

Instance Attribute Details

#data_set_pathObject (readonly)

Returns the value of attribute data_set_path.



9
10
11
# File 'lib/paneron/register/raw/item_class.rb', line 9

def data_set_path
  @data_set_path
end

#extensionObject (readonly)

Returns the value of attribute extension.



9
10
11
# File 'lib/paneron/register/raw/item_class.rb', line 9

def extension
  @extension
end

#item_class_nameObject (readonly)

Returns the value of attribute item_class_name.



9
10
11
# File 'lib/paneron/register/raw/item_class.rb', line 9

def item_class_name
  @item_class_name
end

#item_class_pathObject (readonly)

Returns the value of attribute item_class_path.



9
10
11
# File 'lib/paneron/register/raw/item_class.rb', line 9

def item_class_path
  @item_class_path
end

Class Method Details

.validate_item_class_path(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/paneron/register/raw/item_class.rb', line 27

def self.validate_item_class_path(path)
  unless File.exist?(path)
    raise Paneron::Register::Error,
          "Item class path does not exist"
  end
  unless File.directory?(path)
    raise Paneron::Register::Error,
          "Item class path is not a directory"
  end
end

Instance Method Details

#item_lutamlsObject



64
65
66
# File 'lib/paneron/register/raw/item_class.rb', line 64

def item_lutamls
  items.values.map(&:to_lutaml)
end

#item_uuidsObject



45
46
47
48
# File 'lib/paneron/register/raw/item_class.rb', line 45

def item_uuids
  @item_uuids ||= Dir.glob(File.join(item_class_path, "*.#{extension}"))
    .map { |file| File.basename(file, ".#{extension}") }
end

#items(uuid = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/paneron/register/raw/item_class.rb', line 50

def items(uuid = nil)
  if uuid.nil?
    item_uuids.reduce({}) do |acc, uuid|
      acc[uuid] = items(uuid)
      acc
    end
  else
    @items[uuid] ||=
      Paneron::Register::Raw::Item.new(
        item_class_path, uuid
      )
  end
end

#to_lutamlObject



38
39
40
41
42
43
# File 'lib/paneron/register/raw/item_class.rb', line 38

def to_lutaml
  Paneron::Register::ItemClass.new(
    name: item_class_name,
    items: item_lutamls,
  )
end