Class: Tlog::Entity::Log

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_path = nil) ⇒ Log

Returns a new instance of Log.



9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



6
7
8
# File 'lib/tlog/entity/log.rb', line 6

def entries
  @entries
end

#goalObject

Returns the value of attribute goal.



5
6
7
# File 'lib/tlog/entity/log.rb', line 5

def goal
  @goal
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/tlog/entity/log.rb', line 4

def name
  @name
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/tlog/entity/log.rb', line 7

def path
  @path
end

Instance Method Details

#add_entry(current) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/tlog/entity/log.rb', line 84

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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tlog/entity/log.rb', line 18

def create(options)
  unless Dir.exists?(@path)
    FileUtils.mkdir_p(@path)
    state = 'open'
    points = 0
    owner = 'none'
    state = options[:state] if options[:state]
    points = options[:points] if options[:point]
    owner = options[:owner] if options[:owner]
    write_log(state, points, owner)
    true
  end
end

#deleteObject



103
104
105
# File 'lib/tlog/entity/log.rb', line 103

def delete
  FileUtils.rm_rf(@path) if Dir.exists?(@path)
end

#durationObject



52
53
54
55
56
57
58
# File 'lib/tlog/entity/log.rb', line 52

def duration
  dur = 0
  entries.each do |entry|
    dur += entry.length
  end
  dur
end

#goal_lengthObject



32
33
34
35
36
37
38
# File 'lib/tlog/entity/log.rb', line 32

def goal_length
  if File.exists?(goal_path)
    contents = File.read(goal_path)
    contents.strip
    contents.to_i
  end
end

#ownerObject



60
61
62
# File 'lib/tlog/entity/log.rb', line 60

def owner
  read_file(owner_path) if File.exists?(owner_path)
end

#pointsObject



68
69
70
# File 'lib/tlog/entity/log.rb', line 68

def points
  read_file(points_path) if File.exists?(points_path)
end

#stateObject



64
65
66
# File 'lib/tlog/entity/log.rb', line 64

def state
  read_file(state_path) if File.exists?(state_path)
end

#update_goal(entry_length) ⇒ Object



98
99
100
101
# File 'lib/tlog/entity/log.rb', line 98

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) ⇒ Object



94
95
96
# File 'lib/tlog/entity/log.rb', line 94

def update_head(entry_hex)
  File.open(head_path, 'w'){|f| f.write(entry_hex)}
end

#update_owner(owner) ⇒ Object



80
81
82
# File 'lib/tlog/entity/log.rb', line 80

def update_owner(owner)
  File.open(owner_path, 'w'){|f| f.write(owner)}
end

#update_points(points) ⇒ Object



76
77
78
# File 'lib/tlog/entity/log.rb', line 76

def update_points(points)
  File.open(points_path, 'w'){|f| f.write(points)}
end

#update_state(state) ⇒ Object



72
73
74
# File 'lib/tlog/entity/log.rb', line 72

def update_state(state)
  File.open(state_path, 'w'){|f| f.write(state)}
end