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

Inherits:
Object
  • Object
show all
Includes:
Paneron::Register::RootFinder, Validatable, Writeable
Defined in:
lib/paneron/register/raw/item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Paneron::Register::RootFinder

#git_client, #git_url, #register

Methods included from Validatable

#errors, #path_valid?, #valid?

Methods included from Writeable

#add_changes_to_staging, #commit_changes, #has_uncommited_changes?, #has_unsynced_changes?, #pull_from_remote, #push_commits_to_remote, #remote?, #remote_branch_name, #save, #sync

Constructor Details

#initialize(item_uuid = nil, item_class_path = nil, extension = "yaml", item_class: nil) ⇒ Item

Returns a new instance of Item.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/paneron/register/raw/item.rb', line 17

def initialize(
  item_uuid = nil,
  item_class_path = nil,
  extension = "yaml",
  item_class: nil
)
  # Deduce parent from self path,
  # if only self path is specified.
  @item_class = if item_class.nil?
                  Paneron::Register::Raw::ItemClass.new(
                    item_class_path,
                  )
                else
                  item_class
                end

  require "securerandom"
  require "uuid"

  @item_uuid = if item_uuid.nil? || item_uuid.empty?
                 SecureRandom.uuid
               elsif UUID.validate(item_uuid)
                 item_uuid
               else
                 raise Paneron::Register::Error,
                       "Specified UUID not valid (#{item_uuid})"
               end

  @extension = extension
  @to_h = nil
  @status = nil
  @data = nil
  @date_accepted = nil
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



13
14
15
# File 'lib/paneron/register/raw/item.rb', line 13

def extension
  @extension
end

#item_classObject

Returns the value of attribute item_class.



15
16
17
# File 'lib/paneron/register/raw/item.rb', line 15

def item_class
  @item_class
end

#item_uuidObject (readonly)

Returns the value of attribute item_uuid.



13
14
15
# File 'lib/paneron/register/raw/item.rb', line 13

def item_uuid
  @item_uuid
end

Class Method Details

.nameObject



111
112
113
# File 'lib/paneron/register/raw/item.rb', line 111

def self.name
  "Item"
end

.validate_path(path) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/paneron/register/raw/item.rb', line 139

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

.validate_path_before_savingObject



135
136
137
# File 'lib/paneron/register/raw/item.rb', line 135

def self.validate_path_before_saving
  true
end

Instance Method Details

#dataObject



82
83
84
# File 'lib/paneron/register/raw/item.rb', line 82

def data
  @data ||= disk_to_h["data"]
end

#data=(new_data) ⇒ Object



86
87
88
# File 'lib/paneron/register/raw/item.rb', line 86

def data=(new_data)
  @data = new_data
end

#date_acceptedObject

Default is now



91
92
93
# File 'lib/paneron/register/raw/item.rb', line 91

def date_accepted
  @date_accepted ||= disk_to_h["dateAccepted"] || DateTime.now
end

#date_accepted=(new_date_accepted) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/paneron/register/raw/item.rb', line 95

def date_accepted=(new_date_accepted)
  @date_accepted = case new_date_accepted
                   when String then DateTime.parse(new_date_accepted)
                   when DateTime then new_date_accepted
                   when Time, Date then new_date_accepted.to_datetime
                   when NilClass then nil
                   else
                     raise Paneron::Register::Error,
                           "Invalid date: #{new_date_accepted}"
                   end
end

#disk_to_hObject



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/paneron/register/raw/item.rb', line 168

def disk_to_h
  if File.exist?(item_path)
    @disk_to_h ||=
      YAML.safe_load_file(
        item_path,
        permitted_classes: [Time, Date, DateTime],
      )
  else
    {}
  end
end

#is_valid?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/paneron/register/raw/item.rb', line 127

def is_valid?
  item_class.valid?
end

#item_class_pathObject



56
57
58
# File 'lib/paneron/register/raw/item.rb', line 56

def item_class_path
  @item_class.item_class_path
end

#item_pathObject



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

def item_path
  File.join(item_class_path, "#{item_uuid}.#{extension}")
end

#parentObject



107
108
109
# File 'lib/paneron/register/raw/item.rb', line 107

def parent
  item_class
end

#save_sequenceObject



115
116
117
118
119
120
121
# File 'lib/paneron/register/raw/item.rb', line 115

def save_sequence
  # Save self
  require "fileutils"
  FileUtils.mkdir_p(item_class_path)

  File.write(item_path, to_h.to_yaml)
end

#self_pathObject



123
124
125
# File 'lib/paneron/register/raw/item.rb', line 123

def self_path
  item_path
end

#set(status: nil, data: nil, date_accepted: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/paneron/register/raw/item.rb', line 60

def set(
  status: nil,
  data: nil,
  date_accepted: nil
)
  self.status = status
  self.data = data
  self.date_accepted = date_accepted
end

#set_item_class(new_item_class) ⇒ Object



131
132
133
# File 'lib/paneron/register/raw/item.rb', line 131

def set_item_class(new_item_class)
  @item_class = new_item_class
end

#statusObject



74
75
76
# File 'lib/paneron/register/raw/item.rb', line 74

def status
  @status ||= disk_to_h["status"]
end

#status=(new_status) ⇒ Object



78
79
80
# File 'lib/paneron/register/raw/item.rb', line 78

def status=(new_status)
  @status = new_status
end

#to_hObject



159
160
161
162
163
164
165
166
# File 'lib/paneron/register/raw/item.rb', line 159

def to_h
  {
    "id" => item_uuid,
    "data" => data,
    "status" => status,
    "dateAccepted" => date_accepted,
  }
end

#to_lutamlObject



150
151
152
153
154
155
156
157
# File 'lib/paneron/register/raw/item.rb', line 150

def to_lutaml
  Paneron::Register::Item.new(
    id: item_uuid,
    data: data,
    status: Paneron::Register::ItemStatus.new(state: status),
    date_accepted: date_accepted.to_s,
  )
end

#uuidObject



70
71
72
# File 'lib/paneron/register/raw/item.rb', line 70

def uuid
  @item_uuid
end