Class: Dsu::Models::EntryGroup

Inherits:
Crud::JsonFile show all
Includes:
Support::Fileable, Support::Presentable, Support::TimeComparable, Support::TimeFormatable
Defined in:
lib/dsu/models/entry_group.rb

Overview

This class represents a group of entries for a given day. IOW, things someone might want to share at their daily standup (DSU).

Constant Summary collapse

ENTRIES_FILE_NAME_REGEX =
/\d{4}-\d{2}-\d{2}.json/
ENTRIES_FILE_NAME_TIME_REGEX =
/\d{4}-\d{2}-\d{2}/
VERSION =
Migration::VERSION

Constants included from Support::TimeComparable

Support::TimeComparable::TIME_COMPARABLE_FORMAT_SPECIFIER

Constants included from Support::Fileable

Support::Fileable::MIGRATION_VERSION_FILE_NAME

Instance Attribute Summary collapse

Attributes inherited from Crud::JsonFile

#file_path

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::TimeFormatable

dd_mm_yyyy, formatted_time, mm_dd, mm_dd_yyyy, timezone_for, yyyy_mm_dd, yyyy_mm_dd_or_through_for

Methods included from Support::TimeComparable

#time_equal?, #time_equal_compare_string_for

Methods included from Support::Presentable

#presenter

Methods included from Support::Fileable

#backup_folder, #config_file_name, #config_folder, #config_path, #dsu_folder, #entries_file_name, #entries_folder, #entries_path, #gem_dir, #migration_version_folder, #migration_version_path, #root_folder, #seed_data_folder, #temp_folder, #theme_file_name, #themes_folder, #themes_path

Methods inherited from Crud::JsonFile

file_does_not_exist_message, #file_exist?, file_exist?, parse, #persisted?, read, read!, #reload, #save, #save!, #to_model, #write, #write!

Constructor Details

#initialize(time: nil, entries: nil, version: nil, options: {}) ⇒ EntryGroup

Returns a new instance of EntryGroup.

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dsu/models/entry_group.rb', line 37

def initialize(time: nil, entries: nil, version: nil, options: {})
  raise ArgumentError, 'time is the wrong object type' unless time.is_a?(Time) || time.nil?
  raise ArgumentError, 'version is the wrong object type' unless version.is_a?(Integer) || version.nil?

  FileUtils.mkdir_p(entries_folder)

  @time = ensure_local_time(time)

  super(entries_path(time: @time))

  @version = version || VERSION
  self.entries = entries || []
  @options = options || {}
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



31
32
33
# File 'lib/dsu/models/entry_group.rb', line 31

def entries
  @entries
end

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/dsu/models/entry_group.rb', line 31

def options
  @options
end

#timeObject

Returns the value of attribute time.



30
31
32
# File 'lib/dsu/models/entry_group.rb', line 30

def time
  @time
end

#versionObject

Returns the value of attribute version.



30
31
32
# File 'lib/dsu/models/entry_group.rb', line 30

def version
  @version
end

Class Method Details

.allObject



117
118
119
120
121
122
123
124
125
# File 'lib/dsu/models/entry_group.rb', line 117

def all
  entry_files.filter_map do |file_path|
    entry_file_name = File.basename(file_path)
    next unless entry_file_name.match?(ENTRIES_FILE_NAME_REGEX)

     = File.basename(entry_file_name, '.*')
    find time: Time.parse()
  end
end

.any?Boolean

Returns:



127
128
129
130
131
132
# File 'lib/dsu/models/entry_group.rb', line 127

def any?
  entry_files.any? do |file_path|
     = File.basename(file_path, '.*')
    .match?(ENTRIES_FILE_NAME_TIME_REGEX)
  end
end

.delete(time:) ⇒ Object



134
135
136
# File 'lib/dsu/models/entry_group.rb', line 134

def delete(time:)
  superclass.delete(file_path: entries_path_for(time: time))
end

.delete!(time:) ⇒ Object



138
139
140
# File 'lib/dsu/models/entry_group.rb', line 138

def delete!(time:)
  superclass.delete!(file_path: entries_path_for(time: time))
end

.edit(time:, options: {}) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/dsu/models/entry_group.rb', line 142

def edit(time:, options: {})
  # NOTE: Uncomment this line to prohibit edits on
  # Entry Groups that do not exist (i.e. have no entries).
  # return new(time: time) unless exists?(time: time)

  find_or_initialize(time: time).tap do |entry_group|
    Services::EntryGroup::EditorService.new(entry_group: entry_group, options: options).call
  end
end

.entry_group_times(between: nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dsu/models/entry_group.rb', line 156

def entry_group_times(between: nil)
  entry_files.filter_map do |file_path|
    entry_file_name = File.basename(file_path)
    next unless entry_file_name.match?(ENTRIES_FILE_NAME_REGEX)

    time = File.basename(entry_file_name, '.*')
    next if between && !Time.parse(time).between?(between.min, between.max)

    time
  end
end

.entry_groups(between:) ⇒ Object



168
169
170
171
172
# File 'lib/dsu/models/entry_group.rb', line 168

def entry_groups(between:)
  entry_group_times(between: between).filter_map do |time|
    Models::EntryGroup.find(time: Time.parse(time))
  end
end

.exist?(time:) ⇒ Boolean

Returns:



152
153
154
# File 'lib/dsu/models/entry_group.rb', line 152

def exist?(time:)
  superclass.file_exist?(file_path: entries_path_for(time: time))
end

.find(time:) ⇒ Object



174
175
176
177
178
# File 'lib/dsu/models/entry_group.rb', line 174

def find(time:)
  file_path = entries_path_for(time: time)
  entry_group_hash = read!(file_path: file_path)
  Services::EntryGroup::HydratorService.new(entry_group_hash: entry_group_hash).call
end

.find_or_create(time:) ⇒ Object



180
181
182
183
184
# File 'lib/dsu/models/entry_group.rb', line 180

def find_or_create(time:)
  find_or_initialize(time: time).tap do |entry_group|
    entry_group.write! unless entry_group.exist?
  end
end

.find_or_initialize(time:) ⇒ Object



186
187
188
189
190
191
# File 'lib/dsu/models/entry_group.rb', line 186

def find_or_initialize(time:)
  file_path = entries_path_for(time: time)
  read(file_path: file_path) do |entry_group_hash|
    Services::EntryGroup::HydratorService.new(entry_group_hash: entry_group_hash).call
  end || new(time: time)
end

.write(file_data:, file_path:) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/dsu/models/entry_group.rb', line 193

def write(file_data:, file_path:)
  if file_data[:entries].empty?
    superclass.delete(file_path: file_path)
    return true
  end

  super
end

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



202
203
204
205
206
207
208
209
# File 'lib/dsu/models/entry_group.rb', line 202

def write!(file_data:, file_path:)
  if file_data[:entries].empty?
    superclass.delete!(file_path: file_path)
    return
  end

  super
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Override == and hash so that we can compare Entry Group objects.



53
54
55
56
57
58
59
# File 'lib/dsu/models/entry_group.rb', line 53

def ==(other)
  return false unless other.is_a?(EntryGroup) &&
                      version == other.version &&
                      time_equal?(other_time: other.time)

  entries == other.entries
end

#cloneObject



62
63
64
# File 'lib/dsu/models/entry_group.rb', line 62

def clone
  self.class.new(time: time, entries: entries.map(&:clone), version: version)
end

#deleteObject



66
67
68
69
# File 'lib/dsu/models/entry_group.rb', line 66

def delete
  self.class.delete(time: time)
  entries.clear
end

#delete!Object



71
72
73
74
# File 'lib/dsu/models/entry_group.rb', line 71

def delete!
  self.class.delete!(time: time)
  entries.clear
end

#exist?Boolean

Returns:



85
86
87
# File 'lib/dsu/models/entry_group.rb', line 85

def exist?
  self.class.exist?(time: time)
end

#hashObject



89
90
91
92
93
94
# File 'lib/dsu/models/entry_group.rb', line 89

def hash
  entries.map(&:hash).tap do |hashes|
    hashes << version.hash
    hashes << time_equal_compare_string_for(time: time)
  end.hash
end

#time_formattedObject



96
97
98
# File 'lib/dsu/models/entry_group.rb', line 96

def time_formatted
  formatted_time(time: time)
end

#time_yyyy_mm_ddObject



100
101
102
# File 'lib/dsu/models/entry_group.rb', line 100

def time_yyyy_mm_dd
  yyyy_mm_dd(time: time)
end

#to_hObject



104
105
106
107
108
109
110
# File 'lib/dsu/models/entry_group.rb', line 104

def to_h
  {
    version: version,
    time: time.dup,
    entries: entries.map(&:to_h)
  }
end

#valid_unique_entriesObject



112
113
114
# File 'lib/dsu/models/entry_group.rb', line 112

def valid_unique_entries
  entries&.select(&:valid?)&.uniq(&:description)
end