Class: AppleData::DataFile
- Inherits:
-
Object
- Object
- AppleData::DataFile
show all
- Defined in:
- lib/apple_data/data_file.rb
Overview
Base class for all data files
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.
24
25
26
27
28
29
30
|
# File 'lib/apple_data/data_file.rb', line 24
def initialize(*parts)
@parts = parts
@parts = [self.class.const_get(:DEFAULT_FILENAME)] if @parts.empty?
@collections = {}
load_file(*@parts)
ensure_metadata
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
22
23
24
|
# File 'lib/apple_data/data_file.rb', line 22
def data
@data
end
|
Class Method Details
.all ⇒ Object
18
19
20
|
# File 'lib/apple_data/data_file.rb', line 18
def self.all
@data_files
end
|
.from_path(path) ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/apple_data/data_file.rb', line 32
def self.from_path(path)
instance = DataFile.allocate
instance.instance_eval do
@filename = path
@collections = {}
@data = YAML.load_file @filename if File.exist? @filename
ensure_metadata
end
instance
end
|
.inherited(klass) ⇒ Object
12
13
14
15
16
|
# File 'lib/apple_data/data_file.rb', line 12
def self.inherited(klass)
super
@data_files ||= []
@data_files << klass
end
|
Instance Method Details
#auto_sort? ⇒ Boolean
83
84
85
86
87
|
# File 'lib/apple_data/data_file.rb', line 83
def auto_sort?
value = metadata['auto_sort']
value = true if value.nil?
value
end
|
#collection(name) ⇒ Object
56
57
58
|
# File 'lib/apple_data/data_file.rb', line 56
def collection(name)
@collections[name.to_s] ||= DataFileCollection.new(self, name)
end
|
#collections ⇒ Object
60
61
62
63
|
# File 'lib/apple_data/data_file.rb', line 60
def collections
collection_list = @data['metadata']['collections'] ||= []
collection_list.map { |name| collection(name) }
end
|
#load_file(*parts) ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/apple_data/data_file.rb', line 43
def load_file(*parts)
parts[-1] = "#{parts[-1]}.yaml" unless parts[-1].end_with? '.yaml'
@filename = File.join(AppleData.data_location, File.join(*parts))
@data = {}
@data = YAML.load_file @filename if File.exist? @filename
@data.deep_symbolize_keys!
@data = @data.with_indifferent_access
end
|
89
90
91
|
# File 'lib/apple_data/data_file.rb', line 89
def metadata
@data['metadata'] ||= {}
end
|
#save! ⇒ Object
52
53
54
|
# File 'lib/apple_data/data_file.rb', line 52
def save!
save_data data
end
|
#sort! ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/apple_data/data_file.rb', line 65
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_paths ⇒ Object
93
94
95
|
# File 'lib/apple_data/data_file.rb', line 93
def sort_paths
metadata['sort_paths']
end
|