jtask 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a3724c45e878a1633fa464af954ddefbe729cb8
4
+ data.tar.gz: 23409551f3de8e7290b93864e90240ab0839b016
5
+ SHA512:
6
+ metadata.gz: 0ae4495c34617b63da73c5528114669f677d25642a3ba9ffe31d63d2f02bc1a93f60b33c14ca3764ea40fa754aa770ec1ddccaa279d127e7e4a3efec90c85d11
7
+ data.tar.gz: 8da72a38f76b1f89ac7667949b1039e13429e66b458ac21a764a115f1fee1ae75e7bbbf0da1d4f3428cba203539f72df840063418c1c672af33803ce1ec9d14b
Binary file
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ tmp
3
+ doc
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'jtask'
3
+ s.version = '0.0.1'
4
+ s.platform = Gem::Platform::RUBY
5
+ s.date = '2014-03-23'
6
+ s.summary = "Provides CRUD actions for JSON files, plus a few extra goodies."
7
+ s.description = "CRUD actions for JSON files."
8
+ s.authors = ["Adam McArthur"]
9
+ s.email = 'adam@adammcarthur.net'
10
+ s.homepage = 'https://github.com/adammcarthur/jtask'
11
+ s.license = 'MIT'
12
+ s.files = `git ls-files`.split($/)
13
+ s.require_paths = ["lib"]
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\n- Adam (@adammcarth)\n-"
16
+ end
Binary file
@@ -0,0 +1,27 @@
1
+ ###########################################################################
2
+ # ` JTask v0.0.1 Beta ` #
3
+ # ` Provides CRUD actions for JSON files, plus a few extra goodies. ` #
4
+ # ` Created by Adam McArthur (@adammcarth) ` #
5
+ # ` Released under the MIT licence ` #
6
+ # ` Contribute & Documentation: https://github.com/adammcarthur/jtask ` #
7
+ ###########################################################################
8
+
9
+ class JTask
10
+ # Dependencies
11
+ require "rubygems"
12
+ require "json"
13
+
14
+ # Modules
15
+ modules = ["save", "get", "update", "destroy", "chop", "rename", "kill"]
16
+ modules.each do |m|
17
+ require "jtask/#{m}"
18
+ end
19
+
20
+ extend Save
21
+ extend Get
22
+ extend Update
23
+ extend Destroy
24
+ extend Chop
25
+ extend Rename
26
+ extend Kill
27
+ end
Binary file
@@ -0,0 +1,41 @@
1
+ module Chop
2
+ def chop(filename, id, parameter, dir=nil)
3
+ # Check if user has specified a custom directory.
4
+ unless dir
5
+ # If not, a default folder is assigned.
6
+ dir = "models/"
7
+ end
8
+ original_file = File.read(File.join(dir, filename))
9
+ objects = JSON.parse(original_file)
10
+
11
+ # Quality control - ensure paramter is a string
12
+ unless parameter.is_a?(String)
13
+ raise SyntaxError, "The chop() method can only remove one parameter at a time\n - only a string can be used to specify the parameter.\n\n-"
14
+ end
15
+
16
+ if id.is_a?(Integer)
17
+ if objects["#{id}"]
18
+ # Find object with the id and delete the requested parameter
19
+ removed_version = objects["#{id}"].tap{ |x| x.delete(parameter) }
20
+ insert = objects
21
+ insert["#{id}"] = removed_version
22
+ else
23
+ raise NameError, "\n\nJTask Error => The id #{method} could not be found in the\n file \"#{dir + filename}\". The file has not been changed.\n\n-"
24
+ end
25
+ elsif id == :all
26
+ # Parameter must be removed from all objects
27
+ objects.each do |k, v|
28
+ # Remove parameter from every hash
29
+ objects["#{k}"] = objects["#{k}"].tap{ |x| x.delete(parameter) }
30
+ end
31
+ insert = objects
32
+ else
33
+ raise NameError, "/n/nJTask Error => Incorrect id method used. A single id (integer) can be specified,\n or the symbol \":all\" to remove the parameter from every object.\n\n-"
34
+ end
35
+
36
+ # Re-write the file with the new version.
37
+ File.write(File.join(dir, filename), insert.to_json)
38
+
39
+ return true
40
+ end
41
+ end
@@ -0,0 +1,24 @@
1
+ module Destroy
2
+ def destroy(filename, id, dir=nil)
3
+ # Check if user has specified a custom directory.
4
+ unless dir
5
+ # If not, a default folder is assigned.
6
+ dir = "models/"
7
+ end
8
+ original_file = File.read(File.join(dir, filename))
9
+ objects = JSON.parse(original_file)
10
+
11
+ if objects["#{id}"]
12
+ # Delete the object with id n from the objects hash
13
+ insert = objects.tap{ |x| x.delete("#{id}") }
14
+ else
15
+ # the id doesn't exist in the file
16
+ raise NameError, "\n\nJTask Error => An id of #{id} does not exsist in the file specified at\n \"#{dir + filename}\". Nothing has been deleted.\n\n-"
17
+ end
18
+
19
+ # Re-write the file with the new version.
20
+ File.write(File.join(dir, filename), insert.to_json)
21
+
22
+ return true
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ module Get
2
+ def get(filename, method=nil, dir=nil)
3
+ # Check if user has specified a custom directory.
4
+ unless dir
5
+ # If not, a default folder is assigned.
6
+ dir = "models/"
7
+ end
8
+ original_file = File.read(File.join(dir, filename))
9
+ objects = JSON.parse(original_file)
10
+
11
+ if method
12
+ # Determine if an id integer or {first: - last:} hash method has been supplied.
13
+ if method.is_a?(Integer)
14
+ # Check if 'method' (in this case the single id to retrieve) exists.
15
+ if objects["#{method}"]
16
+ output = {"id" => "#{method}"}.merge(objects["#{method}"])
17
+ # Old objects.select { |k, v| k == "#{method}" }
18
+ else
19
+ # id supplied doesn't exist
20
+ raise NameError, "\n\nJTask Error => The id #{method} could not be found in\n the file \"#{dir + filename}\".\n\n-"
21
+ end
22
+ elsif method.is_a?(Hash)
23
+ if method[:first]
24
+ # Load the FIRST n number of objects from the file
25
+ output = Hash[(objects.to_a).first(method[:first].to_i)]
26
+ elsif method[:last]
27
+ # Load the LAST n number of objects from the file
28
+ output = Hash[(objects.to_a).last(method[:last].to_i).reverse] # wow!
29
+ else
30
+ raise NameError, "\n\nJTask Error => Invalid get method specified. Try using\n JTask.get(\"filename\", last: 10) instead.\n\n-"
31
+ end
32
+ else
33
+ raise NameError, "\n\nJTask Error => Invalid get method specified. Try using\n JTask.get(\"filename\", 1) instead.\n\n-"
34
+ end
35
+ else
36
+ # No retrieval method supplied, so output all the objects from the file :)
37
+ output = objects
38
+ end
39
+
40
+ return output
41
+ end
42
+ end
@@ -0,0 +1,14 @@
1
+ module Kill
2
+ def kill(filename, dir=nil)
3
+ # Check if user has specified a custom directory.
4
+ unless dir
5
+ # If not, a default folder is assigned.
6
+ dir = "models/"
7
+ end
8
+
9
+ # Delete the file
10
+ File.delete(File.join(dir, filename))
11
+
12
+ return true
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Rename
2
+ def rename(filename, new, dir=nil)
3
+ # Check if user has specified a custom directory.
4
+ unless dir
5
+ # If not, a default folder is assigned.
6
+ dir = "models/"
7
+ end
8
+
9
+ # Rename the file
10
+ File.rename(File.join(dir, filename), File.join(dir, new))
11
+
12
+ return true
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ module Save
2
+ def save(filename, parameters, dir=nil)
3
+ # Check if user has specified a custom directory.
4
+ unless dir
5
+ # If not, a default folder is assigned.
6
+ dir = "models/"
7
+ end
8
+ # Check if the file already exists
9
+ unless File.exists?(File.join(dir, filename))
10
+ # Create the file since it doesn’t exist, and setup up for JSON.
11
+ File.open(File.join(dir, filename), "w+") { |file| file.write("{}") }
12
+ end
13
+ # Validate haash supplied for parameters
14
+ unless parameters.is_a?(Hash)
15
+ raise SyntaxError, "\n\nJTask Error => Invalid value supplied to the parameters variable.\n Ensure your parameters are in a symbol hash form\n like - {name: \"Adam\", city: \"Melbourne\"}\n\n-"
16
+ end
17
+
18
+ original_file = File.read(File.join(dir, filename))
19
+ objects = JSON.parse(original_file)
20
+ # Set the id of the new object
21
+ if (objects.to_a).first
22
+ # Add 1 to last object id
23
+ nextid = (objects.to_a).last[0].to_i + 1
24
+ else
25
+ # No current objects exist yet, so set the id to 1
26
+ nextid = 1
27
+ end
28
+
29
+ # Remove last character "}" from file.
30
+ insert = File.read(File.join(dir, filename))[0..-2]
31
+
32
+ insert << "," if (objects.to_a).first # add a comma if at least one object already exists
33
+ insert << "\"#{nextid.to_s}\":"
34
+ insert << parameters.to_json
35
+ # Extra } used to replace the one we took out originally
36
+ insert << "}"
37
+ # Re-write the file with the new version.
38
+ File.write(File.join(dir, filename), insert)
39
+
40
+ return true
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ module Update
2
+ def update(filename, id, parameters, dir=nil)
3
+ # Check if user has specified a custom directory.
4
+ unless dir
5
+ # If not, a default folder is assigned.
6
+ dir = "models/"
7
+ end
8
+ original_file = File.read(File.join(dir, filename))
9
+ objects = JSON.parse(original_file)
10
+
11
+ insert = objects
12
+ parameters.each do |k, v|
13
+ # Update (or add) each parameter
14
+ insert["#{id}"][k.to_s] = v
15
+ end
16
+
17
+ # Re-write the file with the new version.
18
+ File.write(File.join(dir, filename), insert.to_json)
19
+
20
+ return true
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Adam McArthur (@adammcarth)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,117 @@
1
+ # JTask
2
+
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
+
5
+ JTask.save("preferences", {background_color: "black", font_size: "medium"})
6
+ #=> true
7
+
8
+ JTask.get("preferences", 1)["font_size"]
9
+ #=> "medium"
10
+
11
+ 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.
12
+
13
+ **Example of JTask storage file:**
14
+
15
+ {
16
+ "1": {
17
+ "background_colour": "black",
18
+ "font_size": "medium"
19
+ }
20
+ }
21
+
22
+ Sequential ID's are automatically assigned to JSON objects on save.
23
+
24
+ ### JTask.save(filename, parameters, dir=nil)
25
+ *Saves a hash of parameters to the requested file.*
26
+
27
+ JTask.save("foods", {entree: "cheese", main: "hamburger", desert: "cake"})
28
+
29
+ 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`.
30
+
31
+ JTask.save("users.json", {username: "adam", twitter: "@adammcarth"}, "files/")
32
+
33
+ Notes:
34
+
35
+ - **JTask will automatically create the files** for you on save if they don't exist.
36
+ - To prepare already existing files for JTask operations, they must only contain `{}` inside.
37
+ - When setting a custom directory, ensure it ends with `/`.
38
+
39
+ ### JTask.get(filename, method=nil, dir=nil)
40
+ *Retrieves stored JSON data from the file and returns a hash.*
41
+
42
+ JTask.get("email_subscribers")
43
+
44
+ #=> {"1"=>{"email"=>"gary@google.com"}, "2"=>{"email"=>"john@isp.net"}...}
45
+
46
+ 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:
47
+
48
+ @subscriber = JTask.get("email_subscribers", 50)
49
+ #=> {"id"=>"50", "email"=>"yukihiro@matsumoto.net"}
50
+
51
+ @subscriber["email"]
52
+ #=> "yukihiro@matsumoto.net"
53
+
54
+ JTask also comes with a few retrieval methods similar to Active Record. Let's get the **first and last** `n` email subscribers:
55
+
56
+ JTask.get("email_subscribers", first: 25)
57
+ #=> {"1"=>{...}, "2"=>{...} ... "25"=>{...}}
58
+
59
+ JTask.get("email_subscribers", last: 1)
60
+ #=> {"365"=>{..}}
61
+
62
+ ### JTask.update(filename, id, parameters, dir=nil)
63
+ *Updates the record with `id` with a new set of parameters.*
64
+
65
+ JTask.update("ui_settings", 42, {show_ads: "no", background: "grey"})
66
+
67
+ JTask upgrades records gracefully - parameters already existing inside the JSON object will be replaced with the new value, whereas new parameters will be added.
68
+
69
+ # Original Version
70
+ { "42" => { "show_ads": "yes" } }
71
+
72
+ # Updated Version
73
+ { "42" => { "show_ads": "no", "background": "grey" } }
74
+
75
+ To completely remove parameters (the entire key-value pair) from objects, refer to the JTask.chop() method below.
76
+
77
+ ### JTask.destroy(filename, id, dir=nil)
78
+ *Removes an entire object from the file.*
79
+
80
+ JTask.destroy("twitter_names", 15)
81
+
82
+ ### JTask.chop(filename, id, paramter, dir=nil)
83
+ *Removes an entire key-value pair (or parameter) from one or all of the file's objects.*
84
+
85
+ JTask.chop("users", 4, "session_data")
86
+
87
+ JTask.chop("users", :all, "session_data")
88
+
89
+ Impact:
90
+
91
+ # Old Version
92
+ { "4" => { "user_id" => "p18573", "session_data" => "34F3jkdf9azfvVak2" } }
93
+
94
+ # New Version
95
+ { "4" => { "user_id" => "p18573" } }
96
+
97
+ 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).
98
+
99
+ ### JTask.rename(filename, new, dir=nil)
100
+ *Simply renames the file to something different.*
101
+
102
+ JTask.rename("orders", "online_orders")
103
+
104
+ ### JTask.kill(filename, dir=nil)
105
+ *Completely removes the entire file specified from the system.*
106
+
107
+ # Proceed with caution, the will delete the entire
108
+ # file and it cannot be recovered.
109
+ JTask.kill("not_needed_anymore.json")
110
+
111
+ ## Share your ideas and contribute
112
+
113
+ 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).
114
+
115
+ To contribute to the project, fork it, send a pull request and I'll review your changes. Check out the `todo.txt` list to see what still needs to be done.
116
+
117
+ \- Adam
@@ -0,0 +1,3 @@
1
+ 1. Setup a dynamic method handler to make the JTask.get() method cleaner when selecting hash elements. Eg - JTask.get("email_subscribers", 5).email
2
+
3
+ For more information, see ActiveRecord's implementation of method_missing().
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jtask
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam McArthur
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ description: CRUD actions for JSON files.
28
+ email: adam@adammcarthur.net
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".DS_Store"
34
+ - ".gitignore"
35
+ - jtask.gemspec
36
+ - lib/.DS_Store
37
+ - lib/jtask.rb
38
+ - lib/jtask/.DS_Store
39
+ - lib/jtask/chop.rb
40
+ - lib/jtask/destroy.rb
41
+ - lib/jtask/get.rb
42
+ - lib/jtask/kill.rb
43
+ - lib/jtask/rename.rb
44
+ - lib/jtask/save.rb
45
+ - lib/jtask/update.rb
46
+ - license.txt
47
+ - readme.md
48
+ - todo.txt
49
+ homepage: https://github.com/adammcarthur/jtask
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message: |2-
54
+
55
+ Thanks for installing JTask Beta. Check out the full documentation and contribute at https://github.com/adammcarthur/jtask
56
+
57
+ - Adam (@adammcarth)
58
+ -
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Provides CRUD actions for JSON files, plus a few extra goodies.
78
+ test_files: []