Class: Trackstar::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/trackstar/log.rb

Constant Summary collapse

CONFIG_FILE_NAME =
'trackstar.yaml'
POSTS_DIR =
'posts'
DEFAULT_FIELDS =
{ subject: :to_s, hours: :to_f, notes: :to_s }
DEFAULT_FORMATTING =
{ hours: :hr_after }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/trackstar/log.rb', line 12

def initialize
  @config_yaml = load_config_yaml 
  if @config_yaml['post_fields']
    @fields = @config_yaml['post_fields'].transform_keys(&:to_sym).transform_values(&:to_sym)
  else
    @fields = DEFAULT_FIELDS
  end
  if @config_yaml['post_formatting']
    @formatting = @config_yaml['post_formatting'].transform_keys(&:to_sym).transform_values(&:to_sym)
  else
    @formatting = DEFAULT_FORMATTING
  end
  @name = @config_yaml['log_name']
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



10
11
12
# File 'lib/trackstar/log.rb', line 10

def fields
  @fields
end

#formattingObject (readonly)

Returns the value of attribute formatting.



10
11
12
# File 'lib/trackstar/log.rb', line 10

def formatting
  @formatting
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/trackstar/log.rb', line 10

def name
  @name
end

Instance Method Details

#build_postObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/trackstar/log.rb', line 27

def build_post
  new_post = Trackstar::Post.new
  puts "New Post For #{@name}"
  puts "#{new_post.values[:date]}"
  puts "---------------------"
  new_post.fields.each do |key, casting_method|
    begin
      puts "#{key}: "
      new_post.values[key] = gets.chomp.send(casting_method)
    rescue => e
      puts "Sorry, that's not a valid input for #{key}. Let's try this again..."
      retry
    end
  end
  puts ""
  new_post
end

#count_hours(post_list = posts) ⇒ Object



66
67
68
# File 'lib/trackstar/log.rb', line 66

def count_hours(post_list=posts)
  post_list.map { |post| post.values[:hours].to_f }.inject(0, :+)
end

#current_week_hoursObject



74
75
76
# File 'lib/trackstar/log.rb', line 74

def current_week_hours
  count_hours(current_week_posts)
end

#current_week_post_countObject



62
63
64
# File 'lib/trackstar/log.rb', line 62

def current_week_post_count
  current_week_posts.count
end

#current_week_postsObject



51
52
53
54
# File 'lib/trackstar/log.rb', line 51

def current_week_posts
  start_of_week_timestamp = DateTime.now.beginning_of_week.to_time.to_i
  @current_week_posts ||= posts.select { |p| p.values[:timestamp].to_i > start_of_week_timestamp }
end

#post_countObject

stats methods



58
59
60
# File 'lib/trackstar/log.rb', line 58

def post_count
  Dir["#{POSTS_DIR}/*"].count { |file| File.file?(file) }
end

#postsObject



45
46
47
48
49
# File 'lib/trackstar/log.rb', line 45

def posts
  @posts ||= Dir["#{POSTS_DIR}/*.md"].sort.map do |file|
    Trackstar::Post.new(file)
  end
end

#total_hoursObject



70
71
72
# File 'lib/trackstar/log.rb', line 70

def total_hours
  count_hours
end