hacer 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +59 -0
  2. data/lib/hacer.rb +134 -0
  3. data/lib/hacer_version.rb +3 -0
  4. metadata +74 -0
@@ -0,0 +1,59 @@
1
+ = Hacer: A very basic todo-list library
2
+
3
+ Author:: Dave Copeland (mailto:davetron5000 at g mail dot com)
4
+ Copyright:: Copyright (c) 2010 by Dave Copeland
5
+ License:: Distributes under the Apache License, see LICENSE.txt in the source distro
6
+
7
+ * {Source on Github}[http://github.com/davetron5000/hacer]
8
+ * RDoc[http://davetron5000.github.com/hacer]
9
+
10
+ == Use
11
+
12
+ Install if you need to:
13
+
14
+ gem install hacer
15
+
16
+ Hacer provides a few basic features for managing a simple todolist:
17
+
18
+ * <b>Create</b> - Create a new todo item
19
+ * <b>Complete</b> - Compete a todo item
20
+ * <b>List</b> - List todo items
21
+ * <b>Clean</b> - Clear completed items from your todo list
22
+
23
+ === Bootstrapping
24
+
25
+ The main entry into Hacer is Hacer::Todolist. Create one by pointing it to the location where you want your todo list to live:
26
+
27
+ todo_list = Hacer::Todolist.new("todos")
28
+
29
+ === API
30
+
31
+ The Todolist essentially manages a list of Hacer::Todo items
32
+
33
+ todo = todo_list.create("Mow the lawn")
34
+ todo_list.list # => [Todo(1,Mow the lawn)]
35
+
36
+ todo = todo_list.create("Take out the trash")
37
+ todo_list.list # => [Todo(1,Mow the lawn), Todo(2,Take out the trash)]
38
+
39
+ todo.complete(1)
40
+ todo_list.list # => [Todo(2,Take out the trash)]
41
+
42
+ todo_list.list(:all) # => [Todo(1,Mow the lawn,completed), Todo(2,Take out the trash)]
43
+
44
+ todo_list.clean!
45
+ todo_list.list(:all) # => [Todo(2,Take out the trash)]
46
+
47
+ == Notes
48
+
49
+ * API created using {README driven development}[http://tom.preston-werner.com/2010/08/23/readme-driven-development.html]
50
+ * Code created entirely using Test-Driven Development
51
+
52
+ == Development
53
+
54
+ gem install bundler
55
+ bundle install
56
+ rake test
57
+ rake rcov
58
+
59
+
@@ -0,0 +1,134 @@
1
+ require 'yaml'
2
+
3
+ # See Hacer::Todolist
4
+ module Hacer
5
+ # A todo list manages a list of Todo items. It holds all Todo items, even completed ones, until you
6
+ # call clean!
7
+ class Todolist
8
+ # Create a new Todolist stored in the given filename
9
+ #
10
+ # [+filename+] String containing the name of the file to create. If the file exists, it will be parsed as an Hacer todolist. If this isn't possible, an ArgumentError will be raised
11
+ def initialize(filename)
12
+ @filename = filename
13
+ if File.exists?(@filename)
14
+ @todos = File.open(@filename) { |file| YAML.load(file) }
15
+ raise ArgumentError.new("#{@filename} doesn't appear to be an hacer todo list") unless valid_todolist?
16
+ else
17
+ @todos = []
18
+ save_todos
19
+ end
20
+ todo_with_biggest_id = @todos.max { |a,b| a.todo_id <=> b.todo_id }
21
+ @next_id = todo_with_biggest_id.nil? ? 0 : todo_with_biggest_id.todo_id + 1
22
+ end
23
+
24
+ # Create a new todo and store it in this list
25
+ #
26
+ # [+todo_text+] String containing the text of the todo item
27
+ #
28
+ # Returns the Todo item created
29
+ def create(todo_text)
30
+ todo = TodoInternal.new(todo_text,next_id)
31
+ @todos << todo
32
+ save_todos
33
+ todo
34
+ end
35
+
36
+ # Completes this todo item
37
+ #
38
+ # [+todo+] Todo to complete
39
+ def complete(todo)
40
+ todo.complete
41
+ save_todos
42
+ end
43
+
44
+ # Cleans out any completed todos. This cannot be undone an the completed todos will be lost forever
45
+ def clean!
46
+ @todos = self.list(:incomplete)
47
+ save_todos
48
+ end
49
+
50
+ # Return all todos in this Todolist as an Array of Todo
51
+ #
52
+ # [+show+] Symbol representing which todos to return:
53
+ # [+:incomplete+] show only those not completed
54
+ # [+:all+] show everything
55
+ def list(show=:incomplete)
56
+ case show
57
+ when :incomplete then @todos.select { |todo| !todo.completed? }
58
+ when :all then @todos
59
+ else
60
+ raise ArgumentError.new("Only :incomplete or :all are allowed")
61
+ end
62
+ end
63
+
64
+ # Returns the size of the todolist as an Int.
65
+ #
66
+ # [+show+] same as for #list
67
+ def size(show=:incomplete)
68
+ return list(show).size
69
+ end
70
+
71
+ private
72
+
73
+ # Saves the todos into the filename
74
+ def save_todos
75
+ File.open(@filename,'w') do |file|
76
+ YAML.dump(@todos,file)
77
+ end
78
+ end
79
+
80
+ # Returns true if what we parsed into @todos is a valid todo list
81
+ def valid_todolist?
82
+ return false unless @todos.kind_of? Array
83
+ @todos.each do |todo|
84
+ return false unless todo.kind_of? Todo
85
+ end
86
+ true
87
+ end
88
+
89
+ @next_id = 0
90
+ def next_id
91
+ next_id = @next_id
92
+ @next_id += 1
93
+ next_id
94
+ end
95
+ end
96
+
97
+ class Todo
98
+ # Text of the todo
99
+ attr_reader :text
100
+ # id of the todo
101
+ attr_reader :todo_id
102
+
103
+ # Returns true if this todo has been completed
104
+ def completed?; @completed; end
105
+
106
+ # Do not create Todos this way, use the Todolist instead
107
+ def initialize
108
+ raise "Use the Todolist to create todos"
109
+ end
110
+
111
+ def to_s
112
+ s = "Todo(#{todo_id},#{text}"
113
+ s += ",completed" if completed?
114
+ s + ")"
115
+ end
116
+ end
117
+
118
+ # Internal Todo subclass for use only by Todolist; clients of Todolist should program
119
+ # against Todo :nodoc:
120
+ class TodoInternal < Todo #:nodoc:
121
+
122
+ # Create a new todo
123
+ #
124
+ # [+text+] Text for this todo, e.g. "Take out the trash"
125
+ # [+todo_id+] the identifier for this todo
126
+ def initialize(text,todo_id)
127
+ @todo_id = todo_id
128
+ @text = text
129
+ @completed = false
130
+ end
131
+
132
+ def complete; @completed = true; end
133
+ end
134
+ end
@@ -0,0 +1,3 @@
1
+ module Hacer
2
+ VERSION = '0.0.3'
3
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hacer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
11
+ platform: ruby
12
+ authors:
13
+ - David Copeland
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-12 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This is an extremely simple API for creating and managing todo lists. The todo list is a simple YAML file that stores a list of todo items that can be completed/checked off
23
+ email: davidcopeland@naildrivin5.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - lib/hacer.rb
32
+ - lib/hacer_version.rb
33
+ - README.rdoc
34
+ has_rdoc: true
35
+ homepage: http://davetron5000.github.com/hacer
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --title
41
+ - Hacer, the simple todo list API
42
+ - --main
43
+ - README.rdoc
44
+ - -R
45
+ require_paths:
46
+ - lib
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: hacer
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A very simple todo list API
73
+ test_files: []
74
+