Class: Tlog::Entity::Log
- Inherits:
-
Object
- Object
- Tlog::Entity::Log
- Defined in:
- lib/tlog/entity/log.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#goal ⇒ Object
Returns the value of attribute goal.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #add_entry(current) ⇒ Object
- #create ⇒ Object
- #delete ⇒ Object
- #duration ⇒ Object
- #goal_length ⇒ Object
-
#initialize(log_path = nil) ⇒ Log
constructor
A new instance of Log.
- #update_goal(entry_length) ⇒ Object
- #update_head(entry_hex) ⇒ Object
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
#entries ⇒ Object
Returns the value of attribute entries.
6 7 8 |
# File 'lib/tlog/entity/log.rb', line 6 def entries @entries end |
#goal ⇒ Object
Returns the value of attribute goal.
5 6 7 |
# File 'lib/tlog/entity/log.rb', line 5 def goal @goal end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/tlog/entity/log.rb', line 4 def name @name end |
#path ⇒ Object
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
55 56 57 58 59 60 61 62 63 |
# File 'lib/tlog/entity/log.rb', line 55 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 ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/tlog/entity/log.rb', line 47 def create unless Dir.exists?(@path) FileUtils.mkdir_p(@path) File.open(goal_path, 'w'){|f| f.write(@goal)} if @goal true end end |
#delete ⇒ Object
74 75 76 |
# File 'lib/tlog/entity/log.rb', line 74 def delete FileUtils.rm_rf(@path) if Dir.exists?(@path) end |
#duration ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/tlog/entity/log.rb', line 39 def duration dur = 0 entries.each do |entry| dur += entry.length end dur end |
#goal_length ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/tlog/entity/log.rb', line 18 def goal_length if File.exists?(goal_path) contents = File.read(goal_path) contents.strip contents.to_i end end |
#update_goal(entry_length) ⇒ Object
69 70 71 72 |
# File 'lib/tlog/entity/log.rb', line 69 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
65 66 67 |
# File 'lib/tlog/entity/log.rb', line 65 def update_head(entry_hex) File.open(head_path, 'w'){|f| f.write(entry_hex)} end |