jtask 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0851a01dafb0753c4883abf034310c496a3181a8
4
- data.tar.gz: ae407131ec5fd73379f5ffe2deeb327b7578eab1
3
+ metadata.gz: f589f44037f5f3b2e2032dee07d5a47ebea4b3a6
4
+ data.tar.gz: 80d389752aea2d76dafdb73448b89b1ab7998a11
5
5
  SHA512:
6
- metadata.gz: 9de71bd836ab66469769925591e7764ba9753f81d98c7a5e6ca822cb6dafb2ff1f77dbcc13680cdd3e79b894bda54d337a140b2b0ef80e0c8ce3abf43c632673
7
- data.tar.gz: 02e2d26f4009028e2cca6ca74f126ee1eec2bd7599ea4a62aab14c8e58089aceafe7be3b45077221ae81996bb8e404e56191fb99544babe76362a7ad6651b992
6
+ metadata.gz: 1f41a63d10b147aff1c1901e9ea171fcdd05b98f207fb767a7a868b9002a333c2ff0698b7d77dcf9eefa77bfb3293a7e8498eb2862860d76c382c33c4e563f72
7
+ data.tar.gz: b15a34adffb7429425bb5495cfb8e6760959ec2445498ec84a8628348e95c7727d08c53581140e6302a8b8b841c6d5f79cffae7c76976bcae2e0bc91857b118d
data/.gitignore CHANGED
@@ -2,4 +2,4 @@
2
2
  tmp
3
3
  doc
4
4
  .DS_Store
5
- *.sublime-workspace
5
+ *.sublime*
@@ -1,14 +1,3 @@
1
- 0.1.0 [Beta] [Current Version]
2
- =====================================
3
- - Released a major bug fix regarding the `JTask.get()` method (see: https://github.com/adammcarthur/jtask/issues/1)
4
- - Minor code enhancements to the Get module.
5
- - Error messages have been reformatted for both web and console use.
1
+ Moved changelist to:
6
2
 
7
- 0.0.2 [Beta]
8
- =====================================
9
- - Swapped gemspec description and summary (mix up error).
10
-
11
-
12
- 0.0.1 [Beta]
13
- =====================================
14
- - Initial release
3
+ https://github.com/adammcarthur/jtask/releases
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jtask'
3
- s.version = '0.1.0'
3
+ s.version = '0.2.0'
4
4
  s.platform = Gem::Platform::RUBY
5
- s.date = '2014-03-23'
5
+ s.date = '2014-04-01'
6
6
  s.description = "Provides CRUD actions for JSON files, plus a few extra goodies."
7
7
  s.summary = "CRUD actions for JSON files."
8
8
  s.authors = ["Adam McArthur"]
@@ -12,5 +12,5 @@ Gem::Specification.new do |s|
12
12
  s.files = `git ls-files`.split($/)
13
13
  s.require_paths = ["lib"]
14
14
  s.add_runtime_dependency "json", "~> 1.4"
15
- s.post_install_message = "\nThanks for installing JTask Beta. Check out the full documentation and contribute at https://github.com/adammcarthur/jtask\n\nIn this release (v0.1.0 Beta): Fixed a major flaw in the JTask.get() output. Read more about this error here: https://github.com/adammcarthur/jtask/issues/1\n\n- Adam (@adammcarth)\n-"
15
+ s.post_install_message = "\nThanks for installing JTask Beta. Check out the full documentation and contribute at https://github.com/adammcarthur/jtask\n\nIn this release (v0.2.0 Beta): JTask now outputs OpenStructs instead of Hashes. Read more about this update here: https://github.com/adammcarthur/jtask/issues/2\n\n- Adam (@adammcarth)\n-"
16
16
  end
@@ -1,5 +1,5 @@
1
1
  ###########################################################################
2
- # ` JTask v0.1.0 Beta ` #
2
+ # ` JTask v0.2.0 Beta ` #
3
3
  # ` Provides CRUD actions for JSON files, plus a few extra goodies. ` #
4
4
  # ` Created by Adam McArthur (@adammcarth) ` #
5
5
  # ` Released under the MIT licence ` #
@@ -9,6 +9,7 @@
9
9
  class JTask
10
10
  # Dependencies
11
11
  require "rubygems"
12
+ require "ostruct"
12
13
  require "json"
13
14
 
14
15
  # Modules
@@ -3,7 +3,11 @@ module Chop
3
3
  # Check if user has specified a custom directory.
4
4
  unless dir
5
5
  # If not, a default folder is assigned.
6
- dir = "models/"
6
+ if File.directory?("models/")
7
+ dir = "models/"
8
+ else
9
+ raise RuntimeError, "[JTask] The directory 'models/' doesn't exist in your current location. Please create it or refer to the documentation on how to change your file path."
10
+ end
7
11
  end
8
12
  original_file = File.read(File.join(dir, filename))
9
13
  objects = JSON.parse(original_file)
@@ -3,11 +3,15 @@ module Destroy
3
3
  # Check if user has specified a custom directory.
4
4
  unless dir
5
5
  # If not, a default folder is assigned.
6
- dir = "models/"
6
+ if File.directory?("models/")
7
+ dir = "models/"
8
+ else
9
+ raise RuntimeError, "[JTask] The directory 'models/' doesn't exist in your current location. Please create it or refer to the documentation on how to change your file path."
10
+ end
7
11
  end
8
12
  original_file = File.read(File.join(dir, filename))
9
13
  objects = JSON.parse(original_file)
10
-
14
+
11
15
  if objects["#{id}"]
12
16
  # Delete the object with id n from the objects hash
13
17
  insert = objects.tap{ |x| x.delete("#{id}") }
@@ -6,7 +6,11 @@ module Get
6
6
  # Check if user has specified a custom directory.
7
7
  unless dir
8
8
  # If not, a default folder is assigned.
9
- dir = "models/"
9
+ if File.directory?("models/")
10
+ dir = "models/"
11
+ else
12
+ raise RuntimeError, "[JTask] The directory 'models/' doesn't exist in your current location. Please create it or refer to the documentation on how to change your file path."
13
+ end
10
14
  end
11
15
  original_file = File.read(File.join(dir, filename))
12
16
  objects = JSON.parse(original_file)
@@ -21,7 +25,7 @@ module Get
21
25
  id = method
22
26
 
23
27
  if objects["#{id}"]
24
- output = { "id" => id.to_i }.merge(objects["#{id}"])
28
+ output = OpenStruct.new({ "id" => id.to_i }.merge(objects["#{id}"]))
25
29
  else
26
30
  # id supplied doesn't exist
27
31
  raise NameError, "[JTask] The id #{method} could not be found in the file \"#{dir + filename}\"."
@@ -47,11 +51,9 @@ module Get
47
51
  end
48
52
  end
49
53
  # Loop through each required record and
50
- # push each one to the blank output array.
51
- output = []
52
- required_records.each do |id, record|
53
- output.push({ "id" => id.to_i }.merge(record))
54
- end
54
+ # assemble each key-value into the open structure output.
55
+ # Map all openstructs to an array.
56
+ output = required_records.map { |id, record| OpenStruct.new({ "id" => id.to_i }.merge(record)) }
55
57
  end
56
58
 
57
59
  return output
@@ -3,12 +3,16 @@ module Kill
3
3
  # Check if user has specified a custom directory.
4
4
  unless dir
5
5
  # If not, a default folder is assigned.
6
- dir = "models/"
6
+ if File.directory?("models/")
7
+ dir = "models/"
8
+ else
9
+ raise RuntimeError, "[JTask] The directory 'models/' doesn't exist in your current location. Please create it or refer to the documentation on how to change your file path."
10
+ end
7
11
  end
8
12
 
9
13
  # Delete the file
10
14
  File.delete(File.join(dir, filename))
11
-
15
+
12
16
  return true
13
17
  end
14
18
  end
@@ -3,9 +3,13 @@ module Rename
3
3
  # Check if user has specified a custom directory.
4
4
  unless dir
5
5
  # If not, a default folder is assigned.
6
- dir = "models/"
6
+ if File.directory?("models/")
7
+ dir = "models/"
8
+ else
9
+ raise RuntimeError, "[JTask] The directory 'models/' doesn't exist in your current location. Please create it or refer to the documentation on how to change your file path."
10
+ end
7
11
  end
8
-
12
+
9
13
  # Rename the file
10
14
  File.rename(File.join(dir, filename), File.join(dir, new))
11
15
 
@@ -3,7 +3,11 @@ module Save
3
3
  # Check if user has specified a custom directory.
4
4
  unless dir
5
5
  # If not, a default folder is assigned.
6
- dir = "models/"
6
+ if File.directory?("models/")
7
+ dir = "models/"
8
+ else
9
+ raise RuntimeError, "[JTask] The directory 'models/' doesn't exist in your current location. Please create it or refer to the documentation on how to change your file path."
10
+ end
7
11
  end
8
12
  # Check if the file already exists
9
13
  unless File.exists?(File.join(dir, filename))
@@ -14,7 +18,7 @@ module Save
14
18
  unless parameters.is_a?(Hash)
15
19
  raise SyntaxError, "[JTask] Invalid value supplied to the parameters variable. Ensure your parameters are in the symbol hash form, eg - {name: \"Adam\", city: \"Melbourne\"}"
16
20
  end
17
-
21
+
18
22
  original_file = File.read(File.join(dir, filename))
19
23
  objects = JSON.parse(original_file)
20
24
  # Set the id of the new object
@@ -25,10 +29,10 @@ module Save
25
29
  # No current objects exist yet, so set the id to 1
26
30
  nextid = 1
27
31
  end
28
-
32
+
29
33
  # Remove last character "}" from file.
30
34
  insert = File.read(File.join(dir, filename))[0..-2]
31
-
35
+
32
36
  insert << "," if (objects.to_a).first # add a comma if at least one object already exists
33
37
  insert << "\"#{nextid.to_s}\":"
34
38
  insert << parameters.to_json
@@ -36,7 +40,7 @@ module Save
36
40
  insert << "}"
37
41
  # Re-write the file with the new version.
38
42
  File.write(File.join(dir, filename), insert)
39
-
43
+
40
44
  return true
41
45
  end
42
46
  end
@@ -3,7 +3,11 @@ module Update
3
3
  # Check if user has specified a custom directory.
4
4
  unless dir
5
5
  # If not, a default folder is assigned.
6
- dir = "models/"
6
+ if File.directory?("models/")
7
+ dir = "models/"
8
+ else
9
+ raise RuntimeError, "[JTask] The directory 'models/' doesn't exist in your current location. Please create it or refer to the documentation on how to change your file path."
10
+ end
7
11
  end
8
12
  original_file = File.read(File.join(dir, filename))
9
13
  objects = JSON.parse(original_file)
@@ -13,7 +17,7 @@ module Update
13
17
  # Update (or add) each parameter
14
18
  insert["#{id}"][k.to_s] = v
15
19
  end
16
-
20
+
17
21
  # Re-write the file with the new version.
18
22
  File.write(File.join(dir, filename), insert.to_json)
19
23
 
data/readme.md CHANGED
@@ -2,114 +2,156 @@
2
2
 
3
3
  JTask provides CRUD actions for storage of data in JSON format inside text files. It's very useful for times when databases cannot be used to store data, or a simple storage & retrieval mechanism is required. It can act just like a database, check it out:
4
4
 
5
- require "jtask"
6
-
7
- JTask.save("preferences", {background_color: "black", font_size: "medium"})
8
- #=> true
5
+ ``` ruby
6
+ require "jtask"
9
7
 
10
- JTask.get("preferences", 1)["font_size"]
11
- #=> "medium"
8
+ JTask.save("preferences", {background_color: "black", font_size: "medium"})
9
+ #=> true
12
10
 
13
- The above example stores the settings in a file called `preferences` using JSON. We then get the object from the file with id `1`, and output the value of the `"font_size"` key.
11
+ JTask.get("preferences", last: 1).font_size
12
+ #=> "medium"
13
+ ```
14
14
 
15
- **Example of JTask storage file:**
16
-
17
- {
18
- "1": {
19
- "background_colour": "black",
20
- "font_size": "medium"
21
- }
22
- }
15
+ The above example stores the settings in a file called `preferences` using JSON. We then get the last object saved to the file and output the value of the `"font_size"` key.
23
16
 
24
- Sequential ID's are automatically assigned to JSON objects on save.
17
+ **Example of JTask storage file:**
25
18
 
26
- ### JTask.save(filename, parameters, dir=nil)
19
+ ``` json
20
+ {
21
+ "1": {
22
+ "background_colour": "black",
23
+ "font_size": "medium"
24
+ }
25
+ }
26
+ ```
27
+
28
+ JTask can even act as a management system for already exisiting json files. Please note that a few adjustments will need to be made to your files beforehand - check out the [JTask.convert()](https://github.com/adammcarthur/jtask/wiki/JTask.convert() "Configure existing json files for JTask") wiki guide for more information.
29
+
30
+ ## Getting Started
31
+ ``` bash
32
+ gem install jtask
33
+ ```
34
+
35
+ ``` ruby
36
+ # Include the jtask library where necassary
37
+ require "jtask"
38
+
39
+ # Tell JTask where your files are [optional]
40
+ jtask.settings do |s|
41
+ s.file_dir = "path/to/jtask_files"
42
+ end
43
+ ```
44
+
45
+ ### [JTask.save(filename, parameters, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.save() "View full guide")
27
46
  *Saves a hash of parameters to the requested file.*
28
47
 
29
- JTask.save("foods", {entree: "cheese", main: "hamburger", desert: "cake"})
30
-
48
+ ``` ruby
49
+ JTask.save("foods", {entree: "cheese", main: "hamburger", desert: "cake"})
50
+ ```
51
+
31
52
  You can use file extensions if you want (makes no difference), as well as set a custom directory for the file. The default directory JTask will look in is `/models`.
32
53
 
33
- JTask.save("users.json", {username: "adam", twitter: "@adammcarth"}, "files/")
34
-
54
+ ``` ruby
55
+ JTask.save("users.json", {username: "adam", twitter: "@adammcarth"}, "files/")
56
+ ```
57
+
35
58
  Notes:
36
59
 
37
60
  - **JTask will automatically create the files** for you on save if they don't exist.
38
61
  - To prepare already existing files for JTask operations, they must only contain `{}` inside.
39
62
  - When setting a custom directory, ensure it ends with `/`.
40
63
 
41
- ### JTask.get(filename, method=nil, dir=nil)
42
- *Retrieves stored JSON data from the file and returns a hash.*
64
+ ### [JTask.get(filename, method=nil, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.get() "View full guide")
65
+ *Retrieves stored JSON data from the file and returns an OpenStruct.*
66
+
67
+ ``` ruby
68
+ JTask.get("email_subscribers")
69
+ #=> [ <OpenStruct "id"=1 "email"=>"gary@google.com">, <OpenStruct "id"=>2 "email"=>"blah"> ... ]
70
+ ```
43
71
 
44
- JTask.get("email_subscribers")
45
-
46
- #=> {"1"=>{"email"=>"gary@google.com"}, "2"=>{"email"=>"john@isp.net"}...}
47
-
48
72
  As seen above - calling JTask.get() without a method argument will return **all** the records stored. Let's now try and get the 50th email subscriber's email address:
49
73
 
50
- @subscriber = JTask.get("email_subscribers", 50)
51
- #=> {"id"=>"50", "email"=>"yukihiro@matsumoto.net"}
52
-
53
- @subscriber["email"]
54
- #=> "yukihiro@matsumoto.net"
55
-
74
+ ``` ruby
75
+ @subscriber = JTask.get("email_subscribers", 50)
76
+ #=> <OpenStruct "id"="50" "email"="yukihiro@matsumoto.net">
77
+
78
+ @subscriber.email
79
+ #=> "yukihiro@matsumoto.net"
80
+ ```
81
+
56
82
  JTask also comes with a few retrieval methods similar to Active Record. Let's get the **first and last** `n` email subscribers:
57
83
 
58
- JTask.get("email_subscribers", first: 25)
59
- #=> {"1"=>{...}, "2"=>{...} ... "25"=>{...}}
60
-
61
- JTask.get("email_subscribers", last: 1)
62
- #=> {"365"=>{..}}
84
+ ``` ruby
85
+ JTask.get("email_subscribers", first: 25)
86
+ #=> [ <OpenStruct "id"=1>, <OpenStruct "id"=2>, ..., <OpenStruct "id"=25> ]
87
+
88
+ JTask.get("email_subscribers", last: 1)
89
+ #=> <OpenStruct "id"=365 "email"="goo@goo.gl">
90
+ ```
63
91
 
64
- ### JTask.update(filename, id, parameters, dir=nil)
65
- *Updates the record with `id` with a new set of parameters.*
92
+ ### [JTask.update(filename, id, parameters, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.update() "View full guide")
93
+ *Updates the `id` json object with a new set of parameters.*
66
94
 
67
- JTask.update("ui_settings", 42, {show_ads: "no", background: "grey"})
95
+ ``` ruby
96
+ JTask.update("ui_settings", 42, {show_ads: "no", background: "grey"})
97
+ ```
68
98
 
69
99
  JTask upgrades records gracefully - parameters already existing inside the JSON object will be replaced with the new value, whereas new parameters will be added.
70
100
 
71
- # Original Version
72
- { "42" => { "show_ads": "yes" } }
73
-
74
- # Updated Version
75
- { "42" => { "show_ads": "no", "background": "grey" } }
101
+ ``` ruby
102
+ # Original Version
103
+ <OpenStruct "id"=42 "show_ads"="yes">
104
+
105
+ # Updated Version
106
+ <OpenStruct "id"=42 "show_ads"="no" "background"="grey">
107
+ ```
76
108
 
77
109
  To completely remove parameters (the entire key-value pair) from objects, refer to the JTask.chop() method below.
78
110
 
79
- ### JTask.destroy(filename, id, dir=nil)
111
+ ### [JTask.destroy(filename, id, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.destroy() "View full guide")
80
112
  *Removes an entire object from the file.*
81
113
 
82
- JTask.destroy("twitter_names", 15)
83
-
84
- ### JTask.chop(filename, id, paramter, dir=nil)
114
+ ``` ruby
115
+ JTask.destroy("twitter_names", 15)
116
+ ```
117
+
118
+ ### [JTask.chop(filename, id, paramter, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.chop() "View full guide")
85
119
  *Removes an entire key-value pair (or parameter) from one or all of the file's objects.*
86
120
 
87
- JTask.chop("users", 4, "session_data")
88
-
89
- JTask.chop("users", :all, "session_data")
90
-
121
+ ``` ruby
122
+ JTask.chop("users", 4, "session_data")
123
+
124
+ JTask.chop("users", :all, "session_data")
125
+ ```
126
+
91
127
  Impact:
92
128
 
93
- # Old Version
94
- { "4" => { "user_id" => "p18573", "session_data" => "34F3jkdf9azfvVak2" } }
95
-
96
- # New Version
97
- { "4" => { "user_id" => "p18573" } }
98
-
99
- The second example uses `:all` instead of an id. This would perform the same operation, but to all objects inside the target file (instead of a specific id).
129
+ ``` ruby
130
+ # Old Version
131
+ <OpenStruct "id"=4 "user_id"="p18573" "session_data"="34F3jkdf9azfvVak2">
100
132
 
101
- ### JTask.rename(filename, new, dir=nil)
133
+ # New Version
134
+ <OpenStruct "id"=4 "user_id"="p18573">
135
+ ```
136
+
137
+ The second example uses `:all` instead of an id. This would perform the same operation, but to all objects inside the target file (instead of a specific id). [Make sure you read the Chop wiki page](https://github.com/adammcarthur/jtask/wiki/JTask.chop()) to learn more.
138
+
139
+ ### [JTask.rename(filename, new, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.rename() "View full guide")
102
140
  *Simply renames the file to something different.*
103
141
 
104
- JTask.rename("orders", "online_orders")
105
-
106
- ### JTask.kill(filename, dir=nil)
142
+ ``` ruby
143
+ JTask.rename("orders", "online_orders")
144
+ ```
145
+
146
+ ### [JTask.kill(filename, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.kill() "View full guide")
107
147
  *Completely removes the entire file specified from the system.*
108
148
 
109
- # Proceed with caution, the will delete the entire
110
- # file and it cannot be recovered.
111
- JTask.kill("not_needed_anymore.json")
112
-
149
+ ``` ruby
150
+ # Proceed with caution, the will delete the entire
151
+ # file and it cannot be recovered.
152
+ JTask.kill("not_needed_anymore.json")
153
+ ```
154
+
113
155
  ## Share your ideas and contribute
114
156
 
115
157
  I'd love to here what you plan to use JTask for. [Let me know via twitter](https://twitter.com/adammcarth), or email your thoughts and ideas to [adam@adammcarthur.net](mailto:adam@adammcarthur.net).
data/todo.txt CHANGED
@@ -1,16 +1,3 @@
1
- Current List To Transition From Beta Into Stable:
1
+ To do list moved to "enhancment" issues section on GitHub:
2
2
 
3
- 1. Setup a dynamic method handler to make the JTask.get()
4
- method cleaner when selecting hash elements.
5
-
6
- Eg - JTask.get("email_subscribers", 5).email
7
-
8
- For more information, see ActiveRecord's implementation of method_missing().
9
-
10
- 2. Add an encryption feature?
11
-
12
- 3. Allow users to set the default directory.
13
-
14
- 4. Clean up repeated code!!!
15
-
16
- 5. Make file writes more efficient (to prevent scaling issues). Currently, files are completely re-written with the new version on save(), update(), chop() and destroy().
3
+ https://github.com/adammcarthur/jtask/issues?labels=enhancement
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jtask
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam McArthur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-23 00:00:00.000000000 Z
11
+ date: 2014-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -52,7 +52,7 @@ post_install_message: |2-
52
52
 
53
53
  Thanks for installing JTask Beta. Check out the full documentation and contribute at https://github.com/adammcarthur/jtask
54
54
 
55
- In this release (v0.1.0 Beta): Fixed a major flaw in the JTask.get() output. Read more about this error here: https://github.com/adammcarthur/jtask/issues/1
55
+ In this release (v0.2.0 Beta): JTask now outputs OpenStructs instead of Hashes. Read more about this update here: https://github.com/adammcarthur/jtask/issues/2
56
56
 
57
57
  - Adam (@adammcarth)
58
58
  -