Class: Complish::Log

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/complish/models/log.rb

Overview

The Complish Log class

Constant Summary collapse

TYPES =

The log types

Examples:

get the detail log type

Complish::Log::TYPE[:detail]
 #=> 1

get the summary log type

Complish::Log::TYPE[:summary]
 #=> 2

get the goal log type

Complish::Log::TYPE[:goal]
 #=> 3

Since:

  • 0.2.0-alpha

Version:

  • 1.0

{
  detail: 1,
  summary: 2,
  goal: 3
}.freeze

Instance Attribute Summary collapse

Validation callbacks collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentString

Returns the content of the log

Examples:

Return the Log content

Complish::Log.first.content
 #=> 'foo'

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

#created_atTime

Returns the timestamp of the log's creation date

Examples:

Return the Log created_at

Complish::Log.first.created_at
 #=> 1977-05-25 21:00:00 -0600

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

#idFixnum

Returns the id of the log

Examples:

Return the Log id

Complish::Log.first.id
 #=> 1

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

#projectComplish::Project?

Returns the project the log belongs_to

Examples:

Return the Log Project

Complish::Log.first.project
 #=> <#Complish::Project>

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

#project_idFixnum?

Returns the id of the log's project

Examples:

Return the Log project_id

Complish::Log.first.project_id
 #=> 1

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

#typeFixnum

Returns the type of the log

Examples:

Return the Log type

Complish::Log.first.type
 #=> 1

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

#updated_atTime

Returns the timestamp of the log's last updated date

Examples:

Return the Log updated_at

Complish::Log.first.updated_at
 #=> 1977-05-25 21:00:00 -0600

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

#userComplish::User

Returns the user the log belongs_to

Examples:

Return the Log User

Complish::Log.first.user
 #=> <#Complish::User>

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

#user_idFixnum

Returns the id of the log's user

Examples:

Return the Log user_id

Complish::Log.first.user_id
 #=> 1

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/complish/models/log.rb', line 59

class Log < Sequel::Model
  plugin :timestamps, update_on_create: true
  plugin :validation_helpers

  # The log types
  # @version 1.0
  # @example get the detail log type
  #  Complish::Log::TYPE[:detail]
  #   #=> 1
  # @example get the summary log type
  #  Complish::Log::TYPE[:summary]
  #   #=> 2
  # @example get the goal log type
  #  Complish::Log::TYPE[:goal]
  #   #=> 3
  # @return [Hash{Symbol=>Fixnum}] the log types
  TYPES = {
    detail: 1,
    summary: 2,
    goal: 3
  }.freeze

  # @!group Validation callbacks
  # If a user is not provided use the user from the current environment
  # @version 1.0
  # @api private
  # @return [nil]
  def before_validation
    self.user ||= Complish::User.find_or_create_from_env
    super
  end

  # Log validations
  # @version 1.0
  # @api private
  # @see http://sequel.jeremyevans.net/rdoc/files/doc/validations_rdoc.html
  # @raise [Sequel::ValidationFailed] if user not specified
  # @raise [Sequel::ValidationFailed] if type not specified
  # @raise [Sequel::ValidationFailed] if content not specified
  # @note a valid log:
  #
  #  * user must be present.
  #  * type must be present.
  #  * content must be present.
  #  * log must be unique to user scoped to day if type summary and project is nil.
  #  * log must be unique to user and project scoped to day if type summary and project is defined.
  # @return [nil]
  def validate
    validates_presence %i(user type content)
    validate_unique_user_day(%i(user type)) if project.nil?
    validate_unique_user_project_day(%i(user project type)) unless project.nil?
    super
  end
  # @!endgroup

  many_to_one :user
  many_to_one :project

  # @!method today
  #  Find all logs from today
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Log.today
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Log.today.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:today) { |l| l.created_at >= Date.today }

  # @!method details
  #  Find all logs with detail type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.details
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.details.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:details, type: TYPES[:detail])

  # @!method summaries
  #  Find all logs with summary type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.summaries
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::log
  #   Complish::Log.summaries.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:summaries, type: TYPES[:summary])

  # @!method goals
  #  Find all logs with goal type
  #  @version 1.0
  #  @see http://www.rubydoc.info/github/evanfarrar/opensprints/Sequel%2FModel%2FClassMethods%3Asubset
  #  @example Get the dataset
  #   Complish::Log.goals
  #    #=> <#Sequel::SQLite::Dataset>
  #  @example Get an array of Complish::Log
  #   Complish::Log.goals.all
  #    #=> [<#Complish::Log>]
  #  @return [Sequel::SQLite::Dataset] dataset of logs found by query
  subset(:goals, type: TYPES[:goal])

  dataset_module do
    # Get all logs for users
    # @version 1.0
    # @example Get the dataset
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  user = Complish::User.first
    #   #=> <#Complish::User>
    #  Complish::Log.for_user(user).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::User] user the user to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_user(user)
      where(user_id: user.id)
    end

    # Get all logs for project
    # @version 1.0
    # @example Get the dataset
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  project = Complish::Project.first
    #   #=> <#Complish::Project>
    #  Complish::Log.for_project(project).all
    #   #=> [<#Complish::Log>]
    # @param [Complish::Project] project the project to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_project(project)
      where(project_id: project.id)
    end

    # Find logs for given date range
    # @version 1.0
    # @example Get the dataset
    #  Complish::Log.for_dates(Date.yesterday..Time.now)
    #   #=> <#Sequel::SQLite::Dataset>
    # @example Get an array of Complish::Log
    #  Complish::Log.for_dates(Date.yesterday..Time.now).all
    #   #=> [<#Complish::Log>]
    # @param [Range] dates the date range to query the dataset for
    # @return [Sequel::SQLite::Dataset] dataset of logs found by query
    def for_dates(dates)
      where(created_at: dates)
    end
  end

  # Create a detail log
  # @version 1.0
  # @example Create a log with detail type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_detail(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_detail(opts)
    opts[:user] ||= Complish::User.find_or_create_from_env
    opts[:type] = TYPES[:detail]
    create(opts)
  end

  # Create a summary log
  # @version 1.0
  # @example Create a log with summary type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_summary(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_summary(opts)
    opts[:type] = TYPES[:summary]
    create(opts)
  end

  # Create a goal log
  # @version 1.0
  # @example Create a log with goal type
  #  user = Complish::User.first
  #   #=> <#Complish::User>
  #  Complish::Log.create_goal(content: 'foo', user: user)
  #   #=> <#Complish::Log>
  # @param [Hash{Symbol=>String, Integer, Time}] opts the options to create the log with
  # @option opts [Complish::User, nil] user the user to create the log for
  # @option opts [Complish::Project, nil] project the project to create the log for
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_goal(opts)
    opts[:type] = TYPES[:goal]
    create(opts)
  end

  # Create an log for project using ENV['PWD']
  # @version 1.0
  # @example Create an log for current project using ENV['PWD']
  #  Complish::Detail.create_for_project_from_env(content: 'foo')
  #   #=> [<#Complish::Detail>]
  # @param [Hash{Symbol=>String, Fixnum, Time}] opts the properties of the accomplisment to create
  # @option opts [Complish::User, nil] user the user the log belongs to. If not provided will default to
  #  the created projects user.
  # @option opts [String] content the content of the log
  # @return [Complish::Log] the created log
  def self.create_for_project_from_env(opts)
    project = Complish::Project.find_or_create_from_env(opts[:user])
    opts[:project] = project
    opts[:user] ||= project.user
    create(opts)
  end

  # Return content as markdown (github flavored)
  # @version 1.0
  # @example Get markdown for a detail log type
  #  log = Complish::Log.details.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "* foo
  #       "
  # @example Get markdown for a summary log type
  #  log = Complish::Log.summaries.first
  #   #=> <#Complish::Log>
  #  log.markdown
  #   #=> "### Summary
  #
  #        foo"
  # @return [String] the markdown of the log
  def markdown
    should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
  end

  private

  def bulleted_markdown(content)
    "* #{content}\n"
  end

  def header_markdown(header, content)
    "### #{header}\n\n#{content}"
  end

  def should_be_bulleted?(type)
    type == TYPES[:detail] || type == TYPES[:goal]
  end

  def validate_unique_user_day(args)
    validates_unique(args, message: 'can only create one summary per day per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end

  def validate_unique_user_project_day(args)
    validates_unique(args, message: 'can only create one summary per day per project per user') do |ds|
      ds.where(created_at: created_at.beginning_of_day..created_at.end_of_day,
               type: TYPES[:summary])
    end
  end
end

Class Method Details

.create_detail(opts) ⇒ Complish::Log

Create a detail log

Examples:

Create a log with detail type

user = Complish::User.first
 #=> <#Complish::User>
Complish::Log.create_detail(content: 'foo', user: user)
 #=> <#Complish::Log>

Options Hash (opts):

  • user (Complish::User, nil)

    the user to create the log for

  • project (Complish::Project, nil)

    the project to create the log for

  • content (String)

    the content of the log

Since:

  • 0.2.0-alpha

Version:

  • 1.0


233
234
235
236
237
# File 'lib/complish/models/log.rb', line 233

def self.create_detail(opts)
  opts[:user] ||= Complish::User.find_or_create_from_env
  opts[:type] = TYPES[:detail]
  create(opts)
end

.create_for_project_from_env(opts) ⇒ Complish::Log

Create an log for project using ENV['PWD']

Examples:

Create an log for current project using ENV['PWD']

Complish::Detail.create_for_project_from_env(content: 'foo')
 #=> [<#Complish::Detail>]

Options Hash (opts):

  • user (Complish::User, nil)

    the user the log belongs to. If not provided will default to the created projects user.

  • content (String)

    the content of the log

Since:

  • 0.2.0-alpha

Version:

  • 1.0


283
284
285
286
287
288
# File 'lib/complish/models/log.rb', line 283

def self.create_for_project_from_env(opts)
  project = Complish::Project.find_or_create_from_env(opts[:user])
  opts[:project] = project
  opts[:user] ||= project.user
  create(opts)
end

.create_goal(opts) ⇒ Complish::Log

Create a goal log

Examples:

Create a log with goal type

user = Complish::User.first
 #=> <#Complish::User>
Complish::Log.create_goal(content: 'foo', user: user)
 #=> <#Complish::Log>

Options Hash (opts):

  • user (Complish::User, nil)

    the user to create the log for

  • project (Complish::Project, nil)

    the project to create the log for

  • content (String)

    the content of the log

Since:

  • 0.2.0-alpha

Version:

  • 1.0


268
269
270
271
# File 'lib/complish/models/log.rb', line 268

def self.create_goal(opts)
  opts[:type] = TYPES[:goal]
  create(opts)
end

.create_summary(opts) ⇒ Complish::Log

Create a summary log

Examples:

Create a log with summary type

user = Complish::User.first
 #=> <#Complish::User>
Complish::Log.create_summary(content: 'foo', user: user)
 #=> <#Complish::Log>

Options Hash (opts):

  • user (Complish::User, nil)

    the user to create the log for

  • project (Complish::Project, nil)

    the project to create the log for

  • content (String)

    the content of the log

Since:

  • 0.2.0-alpha

Version:

  • 1.0


251
252
253
254
# File 'lib/complish/models/log.rb', line 251

def self.create_summary(opts)
  opts[:type] = TYPES[:summary]
  create(opts)
end

Instance Method Details

#before_validationnil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If a user is not provided use the user from the current environment

Since:

  • 0.2.0-alpha

Version:

  • 1.0


86
87
88
89
# File 'lib/complish/models/log.rb', line 86

def before_validation
  self.user ||= Complish::User.find_or_create_from_env
  super
end

#detailsSequel::SQLite::Dataset

Find all logs with detail type

Examples:

Get the dataset

Complish::Log.details
 #=> <#Sequel::SQLite::Dataset>

Get an array of Complish::Log

Complish::Log.details.all
 #=> [<#Complish::Log>]

See Also:

Version:

  • 1.0


141
# File 'lib/complish/models/log.rb', line 141

subset(:details, type: TYPES[:detail])

#goalsSequel::SQLite::Dataset

Find all logs with goal type

Examples:

Get the dataset

Complish::Log.goals
 #=> <#Sequel::SQLite::Dataset>

Get an array of Complish::Log

Complish::Log.goals.all
 #=> [<#Complish::Log>]

See Also:

Version:

  • 1.0


167
# File 'lib/complish/models/log.rb', line 167

subset(:goals, type: TYPES[:goal])

#markdownString

Return content as markdown (github flavored)

Examples:

Get markdown for a detail log type

log = Complish::Log.details.first
 #=> <#Complish::Log>
log.markdown
 #=> "* foo
     "

Get markdown for a summary log type

log = Complish::Log.summaries.first
 #=> <#Complish::Log>
log.markdown
 #=> "### Summary

      foo"

Since:

  • 0.2.0-alpha

Version:

  • 1.0


306
307
308
# File 'lib/complish/models/log.rb', line 306

def markdown
  should_be_bulleted?(type) ? bulleted_markdown(content) : header_markdown('Summary', content)
end

#summariesSequel::SQLite::Dataset

Find all logs with summary type

Examples:

Get the dataset

Complish::Log.summaries
 #=> <#Sequel::SQLite::Dataset>

Get an array of Complish::log

Complish::Log.summaries.all
 #=> [<#Complish::Log>]

See Also:

Version:

  • 1.0


154
# File 'lib/complish/models/log.rb', line 154

subset(:summaries, type: TYPES[:summary])

#todaySequel::SQLite::Dataset

Find all logs from today

Examples:

Get the dataset

Log.today
 #=> <#Sequel::SQLite::Dataset>

Get an array of Complish::Log

Log.today.all
 #=> [<#Complish::Log>]

See Also:

Version:

  • 1.0


128
# File 'lib/complish/models/log.rb', line 128

subset(:today) { |l| l.created_at >= Date.today }

#validatenil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

a valid log:

  • user must be present.
  • type must be present.
  • content must be present.
  • log must be unique to user scoped to day if type summary and project is nil.
  • log must be unique to user and project scoped to day if type summary and project is defined.

Log validations

Raises:

  • (Sequel::ValidationFailed)

    if user not specified

  • (Sequel::ValidationFailed)

    if type not specified

  • (Sequel::ValidationFailed)

    if content not specified

See Also:

Since:

  • 0.2.0-alpha

Version:

  • 1.0


106
107
108
109
110
111
# File 'lib/complish/models/log.rb', line 106

def validate
  validates_presence %i(user type content)
  validate_unique_user_day(%i(user type)) if project.nil?
  validate_unique_user_project_day(%i(user project type)) unless project.nil?
  super
end