Class: Paneron::Register::Raw::Item

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_class_path, item_id, extension = "yaml") ⇒ Item

Returns a new instance of Item.



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

def initialize(
  item_class_path,
  item_id,
  extension = "yaml"
)
  item_path = File.join(item_class_path, "#{item_id}.#{extension}")
  self.class.validate_item_path(item_path)
  @item_class_path = item_class_path
  @item_id = item_id
  @item_path = item_path
  @extension = extension
  @to_h = nil
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



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

def extension
  @extension
end

#item_class_pathObject (readonly)

Returns the value of attribute item_class_path.



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

def item_class_path
  @item_class_path
end

#item_idObject (readonly)

Returns the value of attribute item_id.



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

def item_id
  @item_id
end

#item_pathObject (readonly)

Returns the value of attribute item_path.



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

def item_path
  @item_path
end

Class Method Details

.validate_item_path(path) ⇒ Object



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

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

Instance Method Details

#to_hObject



48
49
50
51
52
53
54
# File 'lib/paneron/register/raw/item.rb', line 48

def to_h
  @to_h ||=
    YAML.safe_load_file(
      File.join(item_path),
      permitted_classes: [Time],
    )
end

#to_lutamlObject



39
40
41
42
43
44
45
46
# File 'lib/paneron/register/raw/item.rb', line 39

def to_lutaml
  Paneron::Register::Item.new(
    id: item_id,
    data: to_h["data"],
    status: Paneron::Register::ItemStatus.new(state: to_h["status"]),
    date_accepted: to_h["dateAccepted"].to_s,
  )
end