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

Inherits:
Object
  • Object
show all
Includes:
Paneron::Register::RootFinder, Validatable, Writeable
Defined in:
lib/paneron/register/raw/item_class.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_class_path = nil, extension = "yaml", data_set: nil) ⇒ ItemClass

Returns a new instance of ItemClass.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/paneron/register/raw/item_class.rb', line 15

def initialize(
  item_class_path = nil,
  extension = "yaml",
  data_set: nil
)

  unless item_class_path.nil?
    data_set_path, item_class_name = Hierarchical.split_path(item_class_path)
  end

  # Deduce parent from self path,
  # if only self path is specified.
  @data_set = if data_set.nil?
                Paneron::Register::Raw::DataSet.new(
                  data_set_path,
                )
              else
                data_set
              end

  @item_class_name = item_class_name
  @old_name = @item_class_name
  @items_uuids = nil
  @items = {}
  @extension = extension
end

Instance Attribute Details

#data_setObject

Returns the value of attribute data_set.



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

def data_set
  @data_set
end

#extensionObject (readonly)

Returns the value of attribute extension.



11
12
13
# File 'lib/paneron/register/raw/item_class.rb', line 11

def extension
  @extension
end

#item_class_nameObject

Returns the value of attribute item_class_name.



11
12
13
# File 'lib/paneron/register/raw/item_class.rb', line 11

def item_class_name
  @item_class_name
end

Class Method Details

.nameObject



62
63
64
# File 'lib/paneron/register/raw/item_class.rb', line 62

def self.name
  "Item class"
end

.validate_path(path) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/paneron/register/raw/item_class.rb', line 113

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

.validate_path_before_savingObject



109
110
111
# File 'lib/paneron/register/raw/item_class.rb', line 109

def self.validate_path_before_saving
  true
end

Instance Method Details

#add_item(*new_items) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/paneron/register/raw/item_class.rb', line 131

def add_item(*new_items)
  new_items = [new_items] unless new_items.is_a?(Enumerable)
  new_items.each do |item|
    item.set_item_class(self)
    @items[item.item_uuid] = item
  end
end

#add_items(new_items) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/paneron/register/raw/item_class.rb', line 101

def add_items(new_items)
  new_items = [new_items] unless new_items.is_a?(Enumerable)
  new_items.each do |item|
    item.set_item_class(self)
    @items[item.uuid] = item
  end
end

#data_set_pathObject



97
98
99
# File 'lib/paneron/register/raw/item_class.rb', line 97

def data_set_path
  data_set.data_set_path
end

#is_valid?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/paneron/register/raw/item_class.rb', line 89

def is_valid?
  data_set.valid?
end

#item_class_pathObject



42
43
44
# File 'lib/paneron/register/raw/item_class.rb', line 42

def item_class_path
  File.join(data_set_path, item_class_name)
end

#item_lutamlsObject



181
182
183
# File 'lib/paneron/register/raw/item_class.rb', line 181

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

#item_uuidsObject



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

def item_uuids
  if @items.empty?
    Dir.glob(File.join(item_class_path, "*.#{extension}"))
      .map { |file| File.basename(file, ".#{extension}") }.to_set
  else
    @items.keys
  end
end

#items(uuid = nil, refresh: false) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/paneron/register/raw/item_class.rb', line 159

def items(uuid = nil, refresh: false)
  if uuid.nil?
    @items = if !refresh && !@items.empty?
               @items
             else
               item_uuids.reduce({}) do |acc, uuid|
                 acc[uuid] = items(uuid)
                 acc
               end
             end
  elsif refresh
    items[uuid]
  else
    @items[uuid] ||=
      Paneron::Register::Raw::Item.new(
        uuid,
        item_class_path,
        item_class: self,
      )
  end
end

#parentObject



58
59
60
# File 'lib/paneron/register/raw/item_class.rb', line 58

def parent
  data_set
end

#save_sequenceObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/paneron/register/raw/item_class.rb', line 66

def save_sequence
  # Save self
  require "fileutils"

  # Move old data set to new path
  old_path = File.join(data_set_path, @old_name)
  if File.directory?(old_path) && @old_name != item_class_name
    FileUtils.mv(old_path, self_path)
    @old_name = item_class_name
  else
    FileUtils.mkdir_p(self_path)
  end

  # Save items
  item_uuids.each do |item_uuid|
    items(item_uuid).save
  end
end

#self_pathObject



85
86
87
# File 'lib/paneron/register/raw/item_class.rb', line 85

def self_path
  item_class_path
end

#set_data_set(new_data_set) ⇒ Object



93
94
95
# File 'lib/paneron/register/raw/item_class.rb', line 93

def set_data_set(new_data_set)
  @data_set = new_data_set
end

#spawn_item(item_uuid = nil) ⇒ Object



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

def spawn_item(item_uuid = nil)
  new_item = Paneron::Register::Raw::Item.new(
    item_uuid,
    item_class: self,
  )

  add_item(new_item)

  new_item
end

#to_lutamlObject



124
125
126
127
128
129
# File 'lib/paneron/register/raw/item_class.rb', line 124

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