list-tool 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34ece5f2e12c07abd8d89e3487fd5b2a38aedb36
4
- data.tar.gz: 32586be177d6ff874d2a70d5110f5253ec1c8d6c
3
+ metadata.gz: e54d09b832771129aff1a928f60b94fbc1d5f17a
4
+ data.tar.gz: 2708ff9a0124406e76d06f7869cafd0bc588b1ef
5
5
  SHA512:
6
- metadata.gz: c82e5dd20dbf585787b50d88b306bfc8dc6a4f265b225624e1f7e94e19515d67c74bbb4fb40f9f7d8dbfd6b738561698adcd52502394f90475e97690f6c8c218
7
- data.tar.gz: 5e975d2ddf492cdbdd531d3feae2167462790764b12c61c7ba833a894ddd788c5dca3591437ba4c47d1f46351aa0d854fcd7b993acb67d829a2cab70d85d85f0
6
+ metadata.gz: e5ab52d6c9a250a70317fb8653b639c64ee861c444aba11998cdccab69021031e0181d8b4ee2eaae88fca83690cd11da8c841d63130a9e059d23025f9688f4fb
7
+ data.tar.gz: b2fdbd71cb43fa0961d30ae2052941392a5e4ca5f06211f2752eabcae08cf010a7883301ad1357e22c39fd8f0dbefc355c5285292fc8a45542927aaa6711f21c
data/README.md CHANGED
@@ -1,22 +1,84 @@
1
1
  # ListTool
2
2
 
3
- This gem may be used to manage lists of text strings (todos, for example) in your app or in console
3
+ This gem may be used to manage lists of text strings (todos, for example) in your ruby application or in a console (honestly, generally in a console).
4
4
 
5
- ## In app
5
+ ## INSTALLATION
6
6
 
7
- In your app do this:
7
+ To install this gem write `gem install list-tool`
8
8
 
9
- require 'list-tool'
9
+ ## IN RUBY APPLICATIONS
10
10
 
11
- ...
11
+ Require list-tool with `require 'list_tool'` command
12
12
 
13
- lister = Lister.new
13
+ #### Creation
14
14
 
15
- I'll describe later how to work with it
15
+ lister = Lister.new # creates empty list
16
16
 
17
- ## In console
17
+ hash = {
18
+ "default" => 0, # optional
19
+ "lists" => [
20
+ {
21
+ "name" => "New list",
22
+ "items" => [ {"text" => "item1"}, {"text" => "item2"} ]
23
+ }
24
+ ]
25
+ }
26
+ lister = Lister.from_hash(hash) # creates list from given hash
27
+ # (for example, if you have already parsed json)
18
28
 
19
- Also gem provides console tool named 'clt' ('console list tool'), which allows you to manage lists in console.
29
+ lister = Lister.from_json( json_string ) # json document must have same
30
+ # structure, as that hash above
31
+
32
+ #### Saving/loading data
33
+
34
+ lister.load ( '/path/to/data.json' ) # reads data from json file
35
+ lister.save( '/path/to/data.json' ) # saves it's data as json
36
+
37
+ #### General methods
38
+
39
+ lister.lists # => { 'todolist' => 3, 'wishlist' => 2 }, digits are numbers
40
+ # of items in list
41
+ lister.list(list_index=nil) # => {name: 'list_name', items: ['item1', 'item2']}
42
+
43
+ For **#list** method, if list index not specified, it will return contents of default list.
44
+
45
+ #### List management methods
46
+
47
+ lister.add_list('name')
48
+ lister.add_list( {'name'=>'todolist', 'items'=> [{'text'=>'item1'}])
49
+ lister.rename_list(list_index, new_name)
50
+ lister.delete_list(list_index)
51
+ lister.clear_list(list_index)
52
+ lister.set_default_list(list_index)
53
+ lister.move_list(list_index, :up) # or :down. moves list in lists list
54
+
55
+ #### Item management methods
56
+
57
+ lister.add_item(item_text, {list: 2}) # 2 is an example list index here
58
+ lister.change_item(item_index, new_text, {list: 2})
59
+ lister.delete_item(item_index, {list: 2})
60
+ lister.move_item(item_index, :up, {list: 2})
61
+
62
+ You can omit {list: list_index} argument, if so commands will affect default list.
63
+
64
+ #### Bonus classes
65
+
66
+ You may also want to use such classes as **ListTool::List**, **ListTool::Item** and **ListTool::Data**.
67
+
68
+ List class responds to all item management methods (**\*\_item**) and few others, like **#clear!**, **#rename**, **#each** (returns each Item) and **#to_json**
69
+
70
+ Data class responds to all list management methods (**\*\_list**) and also has **#to_json** and **#each** (returns each list) methods.
71
+
72
+ #### Errors
73
+
74
+ - all lister.\*_item methods return nil if list was not found.
75
+ - lister.list and lister.\*_list methods raise **ListTool::NoDefaultListError** if list index is not specified and default list is not set
76
+ - #save and #load methods may return **ListTool::FileAccessError** or **ListTool::FileNotFoundError**
77
+ - other errors are generally **ArgumentError** s
78
+
79
+ ## IN CONSOLE
80
+
81
+ This gem provides console tool named '**clt**' ('console list tool'), which allows you to manage lists in console.
20
82
 
21
83
  USAGE: clt COMMAND [OPTIONS]
22
84
 
@@ -33,12 +95,16 @@ Also gem provides console tool named 'clt' ('console list tool'), which allows y
33
95
  -h, --help Print this message
34
96
  -v, --version Print version
35
97
 
36
- clt keeps it's data file in ~/.clt
98
+ clt keeps it's data in **~/.clt/data.json**
37
99
 
38
- ## Notes
100
+ ## NOTES
39
101
 
40
102
  Note that this is my first gem and also my first rspec experiance.
41
103
 
42
- ## Contacts
104
+ ## CONTACTS
43
105
 
44
106
  You can contact me via email: vizvamitra@gmail.com
107
+
108
+ I'll be happy to get your feedback on this tool.
109
+
110
+ Dmitrii Krasnov
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -1,25 +1,21 @@
1
1
  module ListTool
2
2
 
3
3
  class Item
4
- attr_reader :text
4
+ attr_accessor :text
5
5
 
6
6
  def initialize arg
7
7
  if arg.is_a?(String)
8
- @text = arg.gsub('"', "'")
8
+ @text = arg
9
9
  elsif arg.is_a?(Hash)
10
10
  raise(ArgumentError, "item text not found in given hash") unless arg['text'].is_a?(String)
11
- @text = arg["text"].gsub('"', "'")
11
+ @text = arg["text"]
12
12
  else
13
13
  raise(ArgumentError, "argument expected to be Hash or String, #{arg.class} given")
14
14
  end
15
15
  end
16
16
 
17
- def text= new_text
18
- @text = new_text.gsub('"', "'")
19
- end
20
-
21
17
  def to_json
22
- "{\"text\":\"#{@text}\"}"
18
+ "{\"text\":\"#{@text.gsub('"', '\"')}\"}"
23
19
  end
24
20
 
25
21
  end
@@ -7,12 +7,12 @@ module ListTool
7
7
 
8
8
  def initialize(arg=nil)
9
9
  if arg.respond_to?(:to_str)
10
- @name = arg.gsub('"', "'")
10
+ @name = arg
11
11
  @items = []
12
12
  elsif arg.is_a?(Hash)
13
13
  prepare_data(arg)
14
14
 
15
- @name = arg['name'].gsub('"', "'")
15
+ @name = arg['name']
16
16
 
17
17
  @items = []
18
18
  arg['items'].each do |item|
@@ -30,12 +30,12 @@ module ListTool
30
30
  def rename str
31
31
  raise ArgumentError, 'string expected' unless str.is_a?(String)
32
32
  old_name = @name
33
- @name = str.gsub('"', "'")
33
+ @name = str
34
34
  old_name
35
35
  end
36
36
 
37
37
  def to_json
38
- json = "{\"name\":\"#{@name}\",\"items\":["
38
+ json = "{\"name\":\"#{@name.gsub('"', '\"')}\",\"items\":["
39
39
  @items.each do |item|
40
40
  json += item.to_json
41
41
  json += ',' unless item == @items.last
@@ -32,9 +32,7 @@ module ListTool
32
32
 
33
33
  def lists
34
34
  out = {}
35
- @data.each do |list|
36
- out[list.name] = list.items.count
37
- end
35
+ @data.each { |list| out[list.name] = list.items.count }
38
36
  out
39
37
  end
40
38
 
@@ -48,9 +46,7 @@ module ListTool
48
46
  end
49
47
 
50
48
  out = {name: list.name, items: []}
51
- list.items.each do |item|
52
- out[:items] << item.text
53
- end
49
+ list.items.each { |item| out[:items] << item.text }
54
50
  out
55
51
  end
56
52
 
@@ -1,3 +1,3 @@
1
1
  module ListTool
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -37,12 +37,10 @@ describe ListTool::Item do
37
37
  it 'returns correct json' do
38
38
  expect(item.to_json).to eq "{\"text\":\"item1\"}"
39
39
  end
40
- end
41
40
 
42
- describe '#text=' do
43
- it 'substitutes " with \'' do
44
- item.text = 'text with ""'
45
- expect(item.text).to eq "text with ''"
41
+ it 'substitutes " with \"' do
42
+ item = ListTool::Item.new('text with ""')
43
+ expect(item.to_json).to eq '{"text":"text with \"\""}'
46
44
  end
47
45
  end
48
46
 
@@ -216,6 +216,11 @@ describe ListTool::List do
216
216
  json_str = "{\"name\":\"Todolist\",\"items\":[{\"text\":\"item1\"},{\"text\":\"item2\"}]}"
217
217
  expect(list.to_json).to eq json_str
218
218
  end
219
+
220
+ it 'substitutes " with \"' do
221
+ list = ListTool::List.new({"name" => 'name with ""', "items" => []})
222
+ expect(list.to_json).to eq '{"name":"name with \"\"","items":[]}'
223
+ end
219
224
  end
220
225
 
221
226
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: list-tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitrii Krasnov
@@ -184,7 +184,7 @@ rubyforge_project:
184
184
  rubygems_version: 2.2.2
185
185
  signing_key:
186
186
  specification_version: 4
187
- summary: list-tool-1.0.1
187
+ summary: list-tool-1.0.2
188
188
  test_files:
189
189
  - spec/fixtures/data.json
190
190
  - spec/list_tool/app/commands/add_item_command_spec.rb