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_for, #config_file_name, #config_folder, #config_path, #current_project_file, #current_project_file_name, #dsu_folder, #entries_file_name, #entries_folder, #entries_path, #gem_dir, #migration_version_folder, #migration_version_path, #project_file_for, #project_folder_for, #projects_folder, #root_folder, #seed_data_dsu_configuration_for, #seed_data_dsu_folder_for, #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, #update_version!, #write, #write!

Constructor Details

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

Returns a new instance of EntryGroup.

Raises:

  • (ArgumentError)


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

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.



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

def entries
  @entries
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#timeObject

Returns the value of attribute time.



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

def time
  @time
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.allObject



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

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:

  • (Boolean)


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

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

.delete(time:) ⇒ Object



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

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

.delete!(time:) ⇒ Object



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

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

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



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

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



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

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



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

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:

  • (Boolean)


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

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

.find(time:) ⇒ Object



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

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_initialize(time:) ⇒ Object



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

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



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

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



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

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.



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

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

  entries == other.entries
end

#cloneObject



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

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

#deleteObject



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

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

#delete!Object



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

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

#exist?Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

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

#time_formattedObject



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

def time_formatted
  formatted_time(time: time)
end

#time_yyyy_mm_ddObject



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

def time_yyyy_mm_dd
  yyyy_mm_dd(time: time)
end

#to_hObject



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

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

#valid_unique_entriesObject



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

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