Class: Trackstar::Log
- Inherits:
-
Object
- Object
- Trackstar::Log
- 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
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#formatting ⇒ Object
readonly
Returns the value of attribute formatting.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #build_post ⇒ Object
- #count_hours(post_list = posts) ⇒ Object
- #current_week_hours ⇒ Object
- #current_week_post_count ⇒ Object
- #current_week_posts ⇒ Object
-
#initialize ⇒ Log
constructor
A new instance of Log.
-
#post_count ⇒ Object
stats methods.
- #posts ⇒ Object
- #total_hours ⇒ Object
Constructor Details
#initialize ⇒ Log
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
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
10 11 12 |
# File 'lib/trackstar/log.rb', line 10 def fields @fields end |
#formatting ⇒ Object (readonly)
Returns the value of attribute formatting.
10 11 12 |
# File 'lib/trackstar/log.rb', line 10 def formatting @formatting end |
#name ⇒ Object (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_post ⇒ Object
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_hours ⇒ Object
74 75 76 |
# File 'lib/trackstar/log.rb', line 74 def current_week_hours count_hours(current_week_posts) end |
#current_week_post_count ⇒ Object
62 63 64 |
# File 'lib/trackstar/log.rb', line 62 def current_week_post_count current_week_posts.count end |
#current_week_posts ⇒ Object
51 52 53 54 |
# File 'lib/trackstar/log.rb', line 51 def current_week_posts = DateTime.now.beginning_of_week.to_time.to_i @current_week_posts ||= posts.select { |p| p.values[:timestamp].to_i > } end |
#post_count ⇒ Object
stats methods
58 59 60 |
# File 'lib/trackstar/log.rb', line 58 def post_count Dir["#{POSTS_DIR}/*"].count { |file| File.file?(file) } end |
#posts ⇒ Object
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_hours ⇒ Object
70 71 72 |
# File 'lib/trackstar/log.rb', line 70 def total_hours count_hours end |