Sha256: 43e69582c7d1c5acc8844de7fe4e37981bd36a880ef20181a6e71fda7192376e

Contents?: true

Size: 1.62 KB

Versions: 13

Compression:

Stored size: 1.62 KB

Contents

require 'nokogiri'
require 'time'

module TaskManager
  class TaskFile
    FILE_NAME    = '.task'.freeze
    ELEMENT      = 'task'.freeze
    ROOT         = 'root'.freeze
    NEW_NODE     = 'new_tasks'.freeze
    CURRENT_NODE = 'current_tasks'.freeze
    DONE_NODE    = 'done_tasks'.freeze

    def add(task)
      node = create_node(task)
      handle_xml { root_node.add_child(node) }
      Task.new_from_node(node)
    end

    def all
      root_node.css(ELEMENT).map do |node|
        Task.new_from_node(node)
      end
    end

    protected

    def create_node(task)
      node = xml.create_element(ELEMENT)
      node['id']    = new_id
      node['name']  = task.name
      node['score'] = task.score
      node['created_at'] = task.created_at
      node['started_at'] = task.started_at
      node['finished_at'] = task.finished_at
      node
    end

    def new_id
      highest_id + 1
    end

    def highest_id
      root_node.css(ELEMENT).map { |node| node['id'] }.map(&:to_i).max || 0
    end

    def xml
      @xml ||= begin
        create_file(FILE_NAME) unless File.exists?(FILE_NAME)
        File.open(FILE_NAME, 'r') { |f| Nokogiri::XML(f) }
      end
    end

    def root_node
      @root_node ||= begin
        xml.css(ROOT).first || begin
          node = xml.create_element(ROOT)
          xml.add_child(node)
          node
        end
      end
    end

    def create_file(file_name)
      `touch #{file_name}`
    end

    def handle_xml
      yield if block_given?
      save_xml
    end

    def save_xml
      File.open(FILE_NAME, 'w') { |f| f.write(xml.to_s) }
    end

    def time_now
      Time.now
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
task_manager2-0.0.13 lib/task_manager/task_file.rb
task_manager2-0.0.12 lib/task_manager/task_file.rb
task_manager2-0.0.11 lib/task_manager/task_file.rb
task_manager2-0.0.10 lib/task_manager/task_file.rb
task_manager2-0.0.9 lib/task_manager/task_file.rb
task_manager2-0.0.8 lib/task_manager/task_file.rb
task_manager2-0.0.7 lib/task_manager/task_file.rb
task_manager2-0.0.6 lib/task_manager/task_file.rb
task_manager2-0.0.5 lib/task_manager/task_file.rb
task_manager2-0.0.4 lib/task_manager/task_file.rb
task_manager2-0.0.3 lib/task_manager/task_file.rb
task_manager2-0.0.2 lib/task_manager/task_file.rb
task_manager2-0.0.1 lib/task_manager/task_file.rb