class Tlog::Entity::Log

Attributes

entries[RW]
goal[RW]
name[RW]
path[RW]

Public Class Methods

new(log_path = nil) click to toggle source
# File lib/tlog/entity/log.rb, line 9
def initialize(log_path = nil)
  @entries = []
  if log_path
    @name = log_path.basename.to_s
    @path = log_path
    @goal = goal_length
  end
end

Public Instance Methods

add_entry(current) click to toggle source
# File lib/tlog/entity/log.rb, line 87
def add_entry(current)
  entry_hex = generate_random_hex
  new_entry = Tlog::Entity::Entry.new(entry_path(entry_hex), entry_hex)
  head_hex_value ? parent_hex = head_hex_value : parent_hex = "none"

  update_head(entry_hex)
  new_entry.create(parent_hex, current)
  update_goal(new_entry.length) if goal_length
end
create(options) click to toggle source
# File lib/tlog/entity/log.rb, line 18
def create(options)
  unless Dir.exists?(@path)
    # Default time log attribute values
    state = 'open'
    points = 0
    owner = 'none'    
    
    FileUtils.mkdir_p(@path)
    @goal = ChronicDuration.parse(options[:goal]) if options[:goal]
    state = options[:state] if options[:state]
    points = options[:points] if options[:points]
    owner = options[:owner] if options[:owner]
    write_log(state, points, owner)
    true
  end
end
delete() click to toggle source
# File lib/tlog/entity/log.rb, line 106
def delete
  FileUtils.rm_rf(@path) if Dir.exists?(@path)
end
duration() click to toggle source
# File lib/tlog/entity/log.rb, line 55
def duration
  dur = 0
  entries.each do |entry|
    dur += entry.length
  end
  dur
end
goal_length() click to toggle source
# File lib/tlog/entity/log.rb, line 35
def goal_length
  if File.exists?(goal_path)
    contents = File.read(goal_path)
    contents.strip
    contents.to_i
  end
end
owner() click to toggle source
# File lib/tlog/entity/log.rb, line 63
def owner
  read_file(owner_path) if File.exists?(owner_path)
end
points() click to toggle source
# File lib/tlog/entity/log.rb, line 71
def points
  read_file(points_path).to_i if File.exists?(points_path)
end
state() click to toggle source
# File lib/tlog/entity/log.rb, line 67
def state
  read_file(state_path) if File.exists?(state_path)
end
update_goal(entry_length) click to toggle source
# File lib/tlog/entity/log.rb, line 101
def update_goal(entry_length)
  new_length = goal_length - entry_length
  File.open(goal_path, 'w'){|f| f.write(new_length)}
end
update_head(entry_hex) click to toggle source
# File lib/tlog/entity/log.rb, line 97
def update_head(entry_hex)
  File.open(head_path, 'w'){|f| f.write(entry_hex)}
end
update_owner(owner) click to toggle source
# File lib/tlog/entity/log.rb, line 83
def update_owner(owner)
  File.open(owner_path, 'w'){|f| f.write(owner)}
end
update_points(points) click to toggle source
# File lib/tlog/entity/log.rb, line 79
def update_points(points)
  File.open(points_path, 'w'){|f| f.write(points)}
end
update_state(state) click to toggle source
# File lib/tlog/entity/log.rb, line 75
def update_state(state)
  File.open(state_path, 'w'){|f| f.write(state)}
end

Private Instance Methods

entry_path(hex) click to toggle source
# File lib/tlog/entity/log.rb, line 158
def entry_path(hex)
  File.join(@path, hex)
end
generate_random_hex() click to toggle source
# File lib/tlog/entity/log.rb, line 162
def generate_random_hex
  SecureRandom.hex(13)
end
goal_path() click to toggle source
# File lib/tlog/entity/log.rb, line 146
def goal_path
  File.join(@path, 'GOAL')
end
head_hex_value() click to toggle source
# File lib/tlog/entity/log.rb, line 127
def head_hex_value
  if File.exists?(head_path)
    head_content = File.read(head_path)
    head_content.strip if head_content
  end
end
head_path() click to toggle source
# File lib/tlog/entity/log.rb, line 154
def head_path
  File.join(@path, 'HEAD')
end
hold_path() click to toggle source
# File lib/tlog/entity/log.rb, line 150
def hold_path
  File.join(@path, '.HOLD')
end
owner_path() click to toggle source
# File lib/tlog/entity/log.rb, line 142
def owner_path
  File.join(@path, 'OWNER')
end
points_path() click to toggle source
# File lib/tlog/entity/log.rb, line 134
def points_path
  File.join(@path, 'POINTS')
end
read_file(path) click to toggle source
# File lib/tlog/entity/log.rb, line 120
def read_file(path)
  if File.exists?(path)
    contents = File.read(path)
    contents.strip
  end
end
state_path() click to toggle source
# File lib/tlog/entity/log.rb, line 138
def state_path
  File.join(@path, 'STATE')
end
write_log(state, points, owner) click to toggle source
# File lib/tlog/entity/log.rb, line 112
def write_log(state, points, owner)
  File.open(points_path, 'w'){|f| f.write(points)}
  File.open(state_path, 'w'){|f| f.write(state)}
  File.open(owner_path, 'w'){|f| f.write(owner)}
  File.open(hold_path, 'w+'){|f| f.write('hold')}
  File.open(goal_path, 'w'){|f| f.write(@goal)} if @goal
end