Class: AppleData::DataFile

Inherits:
Object
  • Object
show all
Defined in:
lib/apple_data/data_file.rb

Overview

Base class for all data files

Direct Known Subclasses

FactoryDataReset, IOReg, Keybag, Lockdown

Defined Under Namespace

Classes: DataFileCollection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*parts) ⇒ DataFile

Returns a new instance of DataFile.



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

def initialize(*parts)
  @parts = parts
  @collections = {}
  load_file(*parts)
  
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/apple_data/data_file.rb', line 9

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)


68
69
70
71
72
# File 'lib/apple_data/data_file.rb', line 68

def auto_sort?
  value = ['auto_sort']
  value = true if value.nil?
  value
end

#collection(name) ⇒ Object



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

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

#collectionsObject



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

def collections
  collection_list = @data['metadata']['collections'] ||= []
  collection_list.map { |name| collection(name) }
end

#load_file(*parts) ⇒ Object



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

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.with_indifferent_access
end

#metadataObject



74
75
76
# File 'lib/apple_data/data_file.rb', line 74

def 
  @data['metadata'] ||= {}
end

#save!Object



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

def save!
  save_data data
end

#sort!Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/apple_data/data_file.rb', line 50

def sort!
  collections.each(&:sort!)
  return unless sort_paths

  sort_paths.each do |path|
    JsonPath.for(@data).gsub!(path) do |item|
      case item
      when Array
        item.sort
      when Hash
        item.sort_by { |key, _value| key }.to_h
      else
        item
      end
    end
  end
end

#sort_pathsObject



78
79
80
# File 'lib/apple_data/data_file.rb', line 78

def sort_paths
  ['sort_paths']
end