Class: Dsu::Crud::JsonFile

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/dsu/crud/json_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ JsonFile



14
15
16
# File 'lib/dsu/crud/json_file.rb', line 14

def initialize(file_path)
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



12
13
14
# File 'lib/dsu/crud/json_file.rb', line 12

def file_path
  @file_path
end

#versionObject



53
54
55
# File 'lib/dsu/crud/json_file.rb', line 53

def version
  @version ||= read_version
end

Class Method Details

.delete(file_path:) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/dsu/crud/json_file.rb', line 78

def delete(file_path:)
  return false unless file_exist?(file_path: file_path)

  File.delete(file_path)

  true
end

.delete!(file_path:) ⇒ Object



86
87
88
# File 'lib/dsu/crud/json_file.rb', line 86

def delete!(file_path:)
  raise file_does_not_exist_message(file_path: file_path) unless delete(file_path: file_path)
end

.file_does_not_exist_message(file_path:) ⇒ Object



128
129
130
# File 'lib/dsu/crud/json_file.rb', line 128

def file_does_not_exist_message(file_path:)
  "File \"#{file_path}\" does not exist"
end

.file_exist?(file_path:) ⇒ Boolean



74
75
76
# File 'lib/dsu/crud/json_file.rb', line 74

def file_exist?(file_path:)
  File.exist?(file_path)
end

.parse(json) ⇒ Object



90
91
92
93
94
# File 'lib/dsu/crud/json_file.rb', line 90

def parse(json)
  return if json.nil?

  JSON.parse(json, symbolize_names: true)
end

.read(file_path:) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/dsu/crud/json_file.rb', line 96

def read(file_path:)
  json = File.read(file_path) if file_exist?(file_path: file_path)
  hash = parse(json)
  return yield hash if hash && block_given?

  hash
end

.read!(file_path:) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/dsu/crud/json_file.rb', line 104

def read!(file_path:)
  raise file_does_not_exist_message(file_path: file_path) unless file_exist?(file_path: file_path)

  hash = read(file_path: file_path)
  return yield hash if hash && block_given?

  hash
end

.write(file_data:, file_path:) ⇒ Object

Raises:

  • (ArgumentError)


113
114
115
116
117
118
119
# File 'lib/dsu/crud/json_file.rb', line 113

def write(file_data:, file_path:)
  raise ArgumentError, 'file_data is nil' if file_data.nil?
  raise ArgumentError, "file_data is the wrong object type:\"#{file_data}\"" unless file_data.is_a?(Hash)

  file_data = JSON.pretty_generate(file_data)
  File.write(file_path, file_data)
end

.write!(file_data:, file_path:) ⇒ Object



121
122
123
124
125
126
# File 'lib/dsu/crud/json_file.rb', line 121

def write!(file_data:, file_path:)
  # TODO: Should we be raising an error if the file does not exist?
  # raise file_does_not_exist_message(file_path: file_path) unless file_exist?(file_path: file_path)

  write(file_data: file_data, file_path: file_path)
end

Instance Method Details

#deleteObject



22
23
24
# File 'lib/dsu/crud/json_file.rb', line 22

def delete
  self.class.delete(file_path: file_path)
end

#delete!Object



26
27
28
# File 'lib/dsu/crud/json_file.rb', line 26

def delete!
  self.class.delete!(file_path: file_path)
end

#file_exist?Boolean



30
31
32
# File 'lib/dsu/crud/json_file.rb', line 30

def file_exist?
  self.class.file_exist?(file_path: file_path)
end

#persisted?Boolean



34
35
36
# File 'lib/dsu/crud/json_file.rb', line 34

def persisted?
  file_exist?
end

#reloadObject

Override this method to reload data from the file



39
40
41
42
43
# File 'lib/dsu/crud/json_file.rb', line 39

def reload
  @version = read_version

  self
end

#saveObject



70
71
72
73
74
75
# File 'lib/dsu/crud/json_file.rb', line 70

def write
  return false unless valid?

  self.class.write(file_data: to_h, file_path: file_path)
  true
end

#save!Object



71
72
73
74
75
# File 'lib/dsu/crud/json_file.rb', line 71

def write!
  validate!

  self.class.write(file_data: to_h, file_path: file_path)
end

#to_hObject

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/dsu/crud/json_file.rb', line 45

def to_h
  raise NotImplementedError, 'You must implement this method in a your subclass'
end

#to_modelObject



49
50
51
# File 'lib/dsu/crud/json_file.rb', line 49

def to_model
  self
end

#update_version!Object



18
19
20
# File 'lib/dsu/crud/json_file.rb', line 18

def update_version!
  @version = Migration::VERSION
end

#writeObject



57
58
59
60
61
62
# File 'lib/dsu/crud/json_file.rb', line 57

def write
  return false unless valid?

  self.class.write(file_data: to_h, file_path: file_path)
  true
end

#write!Object



64
65
66
67
68
# File 'lib/dsu/crud/json_file.rb', line 64

def write!
  validate!

  self.class.write(file_data: to_h, file_path: file_path)
end