Class: AppleData::DataFile

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/apple_data/data_file.rb

Overview

Base class for all data files

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*parts) ⇒ DataFile

Returns a new instance of DataFile.



10
11
12
13
14
15
# File 'lib/apple_data/data_file.rb', line 10

def initialize(*parts)
  @parts = parts
  @collections = {}
  T.unsafe(self).load_file(*parts)
  
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/apple_data/data_file.rb', line 7

def data
  @data
end

Class Method Details

.from_path(path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/apple_data/data_file.rb', line 18

def self.from_path(path)
  instance = DataFile.allocate
  instance.instance_eval do
    @filename = path
    @collections = {}
    @data = YAML.load_file @filename if File.exist? @filename
    
  end
  instance
end

Instance Method Details

#auto_sort?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/apple_data/data_file.rb', line 54

def auto_sort?
  if @data['metadata']['auto_sort'].nil?
    true
  else
    @data['metadata']['auto_sort']
  end
end

#collection(name) ⇒ Object



46
47
48
# File 'lib/apple_data/data_file.rb', line 46

def collection(name)
  @collections[name.to_s] ||= DataFileCollection.new(self, name)
end

#load_file(*parts) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/apple_data/data_file.rb', line 30

def load_file(*parts)
  parts[-1] = "#{parts[-1]}.yaml" unless T.must(parts[-1]).end_with? '.yaml'
  @filename = File.join(AppleData.data_location, T.unsafe(File).join(*parts))
  @data = {}
  @data = YAML.load_file @filename if File.exist? @filename
  @data
end

#saveObject



42
43
44
# File 'lib/apple_data/data_file.rb', line 42

def save
  save!
end

#save!Object



38
39
40
# File 'lib/apple_data/data_file.rb', line 38

def save!
  save_data data
end

#sort!Object



50
51
52
# File 'lib/apple_data/data_file.rb', line 50

def sort!
  @collections.each_value(&:sort)
end