jtask 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/jtask.gemspec +3 -3
- data/lib/jtask.rb +10 -12
- data/lib/jtask/chop.rb +18 -11
- data/lib/jtask/config.rb +22 -0
- data/lib/jtask/convert.rb +49 -0
- data/lib/jtask/destroy.rb +18 -11
- data/lib/jtask/get.rb +40 -14
- data/lib/jtask/helpers.rb +23 -0
- data/lib/jtask/kill.rb +15 -11
- data/lib/jtask/rename.rb +14 -12
- data/lib/jtask/save.rb +17 -11
- data/lib/jtask/update.rb +18 -11
- data/readme.md +40 -62
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ae14c187755bf9181d003c8bfeb5537fda03973
|
4
|
+
data.tar.gz: cc4a2c726b565aadfefa509e0f39c6dee8c2af68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b31141a40e28673e441de44b125d6cfdcff9f3a876bbe808fe4db6832b23903826b0d79c3397084fca5697823bfb7d73e89fbe682feca8691f40f82892ca50de
|
7
|
+
data.tar.gz: e8df258605043ae890c5b27fde02e78e2982b3f3f6cd03ab649c214d2ce76bab8248f334a8f886cbd2458c2ae4d09bed65c78a27e2123ccf3b3d0152523f197a
|
data/jtask.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'jtask'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.3.0'
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
|
-
s.date = '2014-04-
|
5
|
+
s.date = '2014-04-03'
|
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.
|
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.3.0 Beta): https://github.com/adammcarthur/jtask/releases/tag/v0.3.0\n\n- Adam (@adammcarth)\n-"
|
16
16
|
end
|
data/lib/jtask.rb
CHANGED
@@ -12,17 +12,15 @@ class JTask
|
|
12
12
|
require "ostruct"
|
13
13
|
require "json"
|
14
14
|
|
15
|
-
#
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
# Configuration
|
16
|
+
require "jtask/config"
|
17
|
+
|
18
|
+
# Helpers
|
19
|
+
require "jtask/helpers"
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
extend Rename
|
27
|
-
extend Kill
|
21
|
+
# Tasks
|
22
|
+
tasks = ["save", "get", "update", "destroy", "chop", "rename", "kill", "convert"]
|
23
|
+
tasks.each do |task|
|
24
|
+
require "jtask/#{task}"
|
25
|
+
end
|
28
26
|
end
|
data/lib/jtask/chop.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# JTask.chop()
|
2
|
+
# Removes an entire key-value pair from one or all of the file's objects.
|
3
|
+
# ------------------------------------------------------------------------------
|
4
|
+
# Eg: JTask.chop("user_data.json", :all, "session_dump")
|
5
|
+
# #=> true
|
6
|
+
# ------------------------------------------------------------------------------
|
7
|
+
# See wiki guide for more usage examples...
|
8
|
+
# https://github.com/adammcarthur/jtask/wiki/JTask.chop()
|
9
|
+
|
10
|
+
require "json"
|
11
|
+
require "jtask/helpers"
|
12
|
+
|
13
|
+
class JTask
|
14
|
+
def self.chop(filename, id, parameter, dir=nil)
|
15
|
+
# Set the directory
|
16
|
+
dir = JTask::Helpers.set_directory(dir)
|
17
|
+
|
18
|
+
# Parse the file
|
12
19
|
original_file = File.read(File.join(dir, filename))
|
13
20
|
objects = JSON.parse(original_file)
|
14
21
|
|
data/lib/jtask/config.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class JTask
|
2
|
+
# Provides configuration options for JTask.
|
3
|
+
# Included in `lib/jtask.rb`.
|
4
|
+
class Configuration
|
5
|
+
# Used to set the default directory JTask looks in for files.
|
6
|
+
attr_accessor :file_dir
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
self.file_dir = "models/"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Allow options to be set under the JTask namespace.
|
14
|
+
def self.configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configure
|
19
|
+
# Update Configuration with new values if config the block is given.
|
20
|
+
yield(configuration) if block_given?
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# JTask::Convert
|
2
|
+
# Prepares existing json files to be manipulated by JTask's methods.
|
3
|
+
# -----------------------------------------------------------------------------------
|
4
|
+
# Eg: JTask::Convert.json_file("wordpress_users_export.json")
|
5
|
+
# #=> true
|
6
|
+
# -----------------------------------------------------------------------------------
|
7
|
+
# See wiki guide for more usage examples...
|
8
|
+
# https://github.com/adammcarthur/jtask/wiki/JTask::Convert
|
9
|
+
|
10
|
+
require "json"
|
11
|
+
require "jtask/rename"
|
12
|
+
require "jtask/save"
|
13
|
+
require "jtask/helpers"
|
14
|
+
|
15
|
+
class JTask
|
16
|
+
module Convert
|
17
|
+
def self.json_file(filename, dir=nil)
|
18
|
+
# Set the directory
|
19
|
+
dir = JTask::Helpers.set_directory(dir)
|
20
|
+
|
21
|
+
# Parse the file
|
22
|
+
original_file = File.read(File.join(dir, filename))
|
23
|
+
objects = JSON.parse(original_file)
|
24
|
+
|
25
|
+
output = Hash.new
|
26
|
+
objects.each_with_index do |(k, v), n|
|
27
|
+
output["#{n + 1}"] = {k => v}
|
28
|
+
end
|
29
|
+
|
30
|
+
# Rename the old version
|
31
|
+
begin
|
32
|
+
JTask.rename(filename, "#{filename}.old", dir)
|
33
|
+
rescue
|
34
|
+
raise RuntimeError, "[JTask] Failed to backup original file, conversion aborted."
|
35
|
+
end
|
36
|
+
|
37
|
+
# Write the new version
|
38
|
+
begin
|
39
|
+
JTask.save(filename, output, dir)
|
40
|
+
rescue
|
41
|
+
raise RuntimeError, "[JTask] Failed to save the converted file. The original can be found at '#{dir}#{filename}.old'"
|
42
|
+
end
|
43
|
+
|
44
|
+
return true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# JTask::Convert.json_file("orders.json")
|
data/lib/jtask/destroy.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# JTask.destroy()
|
2
|
+
# Removes an entire object from the file.
|
3
|
+
# ------------------------------------------------------------------
|
4
|
+
# Eg: JTask.destroy("orders.json", 5)
|
5
|
+
# #=> true
|
6
|
+
# ------------------------------------------------------------------
|
7
|
+
# See wiki guide for more usage examples...
|
8
|
+
# https://github.com/adammcarthur/jtask/wiki/JTask.destroy()
|
9
|
+
|
10
|
+
require "json"
|
11
|
+
require "jtask/helpers"
|
12
|
+
|
13
|
+
class JTask
|
14
|
+
def self.destroy(filename, id, dir=nil)
|
15
|
+
# Set the directory
|
16
|
+
dir = JTask::Helpers.set_directory(dir)
|
17
|
+
|
18
|
+
# Parse the file
|
12
19
|
original_file = File.read(File.join(dir, filename))
|
13
20
|
objects = JSON.parse(original_file)
|
14
21
|
|
data/lib/jtask/get.rb
CHANGED
@@ -1,17 +1,43 @@
|
|
1
|
-
# JTask.get()
|
2
|
-
# Retrieves stored JSON data from the file and returns
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
# JTask.get()
|
2
|
+
# Retrieves stored JSON data from the file and returns an OpenStruct.
|
3
|
+
# --------------------------------------------------------------------------------
|
4
|
+
# Eg: JTask.get("test.json", 1) || JTask.get("test.json", first: 10)
|
5
|
+
# #=> <OpenStruct id=x, ...>
|
6
|
+
# --------------------------------------------------------------------------------
|
7
|
+
# See wiki guide for more usage examples...
|
8
|
+
# https://github.com/adammcarthur/jtask/wiki/JTask.get()
|
9
|
+
|
10
|
+
require "json"
|
11
|
+
require "ostruct"
|
12
|
+
require "jtask/helpers"
|
13
|
+
|
14
|
+
class JTask
|
15
|
+
# Allows nested OpenStructs, refer to http://andreapavoni.com/blog/2013/4/create-recursive-openstruct-from-a-ruby-hash
|
16
|
+
# Credits: Andrea Pavoni (DeepStruct guru)
|
17
|
+
class Get < OpenStruct
|
18
|
+
def initialize(hash=nil)
|
19
|
+
@table = {}
|
20
|
+
@hash_table = {}
|
21
|
+
if hash
|
22
|
+
hash.each do |k,v|
|
23
|
+
@table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
|
24
|
+
@hash_table[k.to_sym] = v
|
25
|
+
new_ostruct_member(k)
|
26
|
+
end
|
13
27
|
end
|
14
28
|
end
|
29
|
+
def to_h
|
30
|
+
@hash_table
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class JTask
|
36
|
+
def self.get(filename, method=nil, dir=nil)
|
37
|
+
# Set the directory
|
38
|
+
dir = JTask::Helpers.set_directory(dir)
|
39
|
+
|
40
|
+
# Parse the file
|
15
41
|
original_file = File.read(File.join(dir, filename))
|
16
42
|
objects = JSON.parse(original_file)
|
17
43
|
|
@@ -25,7 +51,7 @@ module Get
|
|
25
51
|
id = method
|
26
52
|
|
27
53
|
if objects["#{id}"]
|
28
|
-
output =
|
54
|
+
output = JTask::Get.new({ "id" => id.to_i }.merge(objects["#{id}"]))
|
29
55
|
else
|
30
56
|
# id supplied doesn't exist
|
31
57
|
raise NameError, "[JTask] The id #{method} could not be found in the file \"#{dir + filename}\"."
|
@@ -53,7 +79,7 @@ module Get
|
|
53
79
|
# Loop through each required record and
|
54
80
|
# assemble each key-value into the open structure output.
|
55
81
|
# Map all openstructs to an array.
|
56
|
-
output = required_records.map { |id, record|
|
82
|
+
output = required_records.map { |id, record| JTask::Get.new({ "id" => id.to_i }.merge(record)) }
|
57
83
|
end
|
58
84
|
|
59
85
|
return output
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "jtask/config"
|
2
|
+
|
3
|
+
class JTask
|
4
|
+
module Helpers
|
5
|
+
def self.set_directory(custom_directory=nil)
|
6
|
+
# Check if user has specified a custom directory in the method
|
7
|
+
if custom_directory
|
8
|
+
# Yes, lets use it.
|
9
|
+
dir = custom_directory
|
10
|
+
else
|
11
|
+
# No, lets load the default directory.
|
12
|
+
dir = JTask.configuration.file_dir
|
13
|
+
end
|
14
|
+
|
15
|
+
# Panic if the directory doesn't exist. Literally.
|
16
|
+
unless File.directory?(dir)
|
17
|
+
raise RuntimeError, "[JTask] The directory '#{dir}' doesn't exist in your current location. Please create it or refer to the documentation on how to change your file path."
|
18
|
+
end
|
19
|
+
|
20
|
+
return dir
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/jtask/kill.rb
CHANGED
@@ -1,14 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# JTask.kill()
|
2
|
+
# Completely removes the entire file specified from the system.
|
3
|
+
# -----------------------------------------------------------------------------
|
4
|
+
# Eg: JTask.kill("users.json.bak")
|
5
|
+
# #=> true
|
6
|
+
# -----------------------------------------------------------------------------
|
7
|
+
# See wiki guide for more usage examples...
|
8
|
+
# https://github.com/adammcarthur/jtask/wiki/JTask.kill()
|
9
|
+
|
10
|
+
require "jtask/helpers"
|
11
|
+
|
12
|
+
class JTask
|
13
|
+
def self.kill(filename, dir=nil)
|
14
|
+
# Set the directory
|
15
|
+
dir = JTask::Helpers.set_directory(dir)
|
12
16
|
|
13
17
|
# Delete the file
|
14
18
|
File.delete(File.join(dir, filename))
|
data/lib/jtask/rename.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# JTask.rename()
|
2
|
+
# Simply renames the file to something different.
|
3
|
+
# ----------------------------------------------------------------------------------
|
4
|
+
# Eg: JTask.rename("orders.json", "0r3erz.json")
|
5
|
+
# #=> true
|
6
|
+
# ----------------------------------------------------------------------------------
|
7
|
+
# See wiki guide for more usage examples...
|
8
|
+
# https://github.com/adammcarthur/jtask/wiki/JTask.rename()
|
9
|
+
|
10
|
+
class JTask
|
11
|
+
def self.rename(filename, new_filename, dir=nil)
|
12
|
+
# Set the directory
|
13
|
+
dir = JTask::Helpers.set_directory(dir)
|
12
14
|
|
13
15
|
# Rename the file
|
14
|
-
File.rename(File.join(dir, filename), File.join(dir,
|
16
|
+
File.rename(File.join(dir, filename), File.join(dir, new_filename))
|
15
17
|
|
16
18
|
return true
|
17
19
|
end
|
data/lib/jtask/save.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# JTask.save()
|
2
|
+
# Saves a hash of parameters to the requested file.
|
3
|
+
# --------------------------------------------------------------------
|
4
|
+
# Eg: JTask.save("hello.json", {fname: "Adam", lname: "McArthur"})
|
5
|
+
# #=> true
|
6
|
+
# --------------------------------------------------------------------
|
7
|
+
# See wiki guide for more usage examples...
|
8
|
+
# https://github.com/adammcarthur/jtask/wiki/JTask.save()
|
9
|
+
|
10
|
+
require "json"
|
11
|
+
require "jtask/helpers"
|
12
|
+
|
13
|
+
class JTask
|
14
|
+
def self.save(filename, parameters, dir=nil)
|
15
|
+
# Set the directory
|
16
|
+
dir = JTask::Helpers.set_directory(dir)
|
17
|
+
|
12
18
|
# Check if the file already exists
|
13
19
|
unless File.exists?(File.join(dir, filename))
|
14
20
|
# Create the file since it doesn’t exist, and setup up for JSON.
|
data/lib/jtask/update.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# JTask.update()
|
2
|
+
# Updates an existing JSON object with a new set of values.
|
3
|
+
# ----------------------------------------------------------------------
|
4
|
+
# Eg: JTask.update("clients.json", 4, new: "value")
|
5
|
+
# #=> true
|
6
|
+
# ----------------------------------------------------------------------
|
7
|
+
# See wiki guide for more usage examples...
|
8
|
+
# https://github.com/adammcarthur/jtask/wiki/JTask.update()
|
9
|
+
|
10
|
+
require "json"
|
11
|
+
require "jtask/helpers"
|
12
|
+
|
13
|
+
class JTask
|
14
|
+
def self.update(filename, id, parameters, dir=nil)
|
15
|
+
# Set the directory
|
16
|
+
dir = JTask::Helpers.set_directory(dir)
|
17
|
+
|
18
|
+
# Parse the file
|
12
19
|
original_file = File.read(File.join(dir, filename))
|
13
20
|
objects = JSON.parse(original_file)
|
14
21
|
|
data/readme.md
CHANGED
@@ -5,14 +5,21 @@ JTask provides CRUD actions for storage of data in JSON format inside text files
|
|
5
5
|
``` ruby
|
6
6
|
require "jtask"
|
7
7
|
|
8
|
-
JTask.save("preferences", {background_color: "black", font_size: "medium"})
|
8
|
+
JTask.save("preferences.json", {background_color: "black", font_size: "medium"})
|
9
9
|
#=> true
|
10
10
|
|
11
|
-
JTask.get("preferences", last: 1).font_size
|
11
|
+
JTask.get("preferences.json", last: 1).font_size
|
12
12
|
#=> "medium"
|
13
13
|
```
|
14
14
|
|
15
|
-
The above example stores the settings in a file called `preferences
|
15
|
+
The above example stores the settings in a file called `preferences.json`. We then get the last object saved to the file and output the value of the `"font_size"` key. JTask syntax is designed to be simple and elegant to use, making it easy to remember and enjoyable to work with. It even has it's own built in error handler:
|
16
|
+
|
17
|
+
``` bash
|
18
|
+
JTask.get("clients.json", 1600)
|
19
|
+
#=> NameError: [JTask] The id 1600 could not be found in the file "models/clients.json".
|
20
|
+
```
|
21
|
+
|
22
|
+
The above error makes mention of an id number. As central to many CRUD frameworks, JTask operates around id numbers to manipulate the objects. JTask automatically calculates and assigns these sequential id numbers for you.
|
16
23
|
|
17
24
|
**Example of JTask storage file:**
|
18
25
|
|
@@ -25,7 +32,7 @@ The above example stores the settings in a file called `preferences` using JSON.
|
|
25
32
|
}
|
26
33
|
```
|
27
34
|
|
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
|
35
|
+
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
36
|
|
30
37
|
## Getting Started
|
31
38
|
``` bash
|
@@ -33,23 +40,26 @@ gem install jtask
|
|
33
40
|
```
|
34
41
|
|
35
42
|
``` ruby
|
36
|
-
# Include the jtask library where necassary
|
37
43
|
require "jtask"
|
38
44
|
|
39
45
|
# Tell JTask where your files are [optional]
|
40
|
-
|
41
|
-
|
46
|
+
JTask.configure do |config|
|
47
|
+
config.file_dir = "path/to/jtask_files/"
|
42
48
|
end
|
43
49
|
```
|
44
50
|
|
45
|
-
|
51
|
+
## Basic Documentation
|
52
|
+
|
53
|
+
Below you will find a very broad overview of how to use the most common JTask methods. Make sure you [check out the wiki](https://github.com/adammcarthur/jtask/wiki) for the full documentation and implementation examples.
|
54
|
+
|
55
|
+
#### [JTask.save(filename, parameters, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.save() "View full guide")
|
46
56
|
*Saves a hash of parameters to the requested file.*
|
47
57
|
|
48
58
|
``` ruby
|
49
|
-
JTask.save("foods", {entree: "cheese", main: "hamburger", desert: "cake"})
|
59
|
+
JTask.save("foods.json", {entree: "cheese", main: "hamburger", desert: "cake"})
|
50
60
|
```
|
51
61
|
|
52
|
-
|
62
|
+
Custom directories can be set each time (refer to below). The default directory JTask will look in is `/models`.
|
53
63
|
|
54
64
|
``` ruby
|
55
65
|
JTask.save("users.json", {username: "adam", twitter: "@adammcarth"}, "files/")
|
@@ -61,19 +71,19 @@ Notes:
|
|
61
71
|
- To prepare already existing files for JTask operations, they must only contain `{}` inside.
|
62
72
|
- When setting a custom directory, ensure it ends with `/`.
|
63
73
|
|
64
|
-
|
74
|
+
#### [JTask.get(filename, method=nil, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.get() "View full guide")
|
65
75
|
*Retrieves stored JSON data from the file and returns an OpenStruct.*
|
66
76
|
|
67
77
|
``` ruby
|
68
|
-
JTask.get("email_subscribers")
|
69
|
-
#=> [ <
|
78
|
+
JTask.get("email_subscribers.json")
|
79
|
+
#=> [ <JTask::Get "id"=1 "email"=>"gary@google.com">, <JTask::Get "id"=>2 "email"=>"blah"> ... ]
|
70
80
|
```
|
71
81
|
|
72
82
|
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:
|
73
83
|
|
74
84
|
``` ruby
|
75
|
-
@subscriber = JTask.get("email_subscribers", 50)
|
76
|
-
#=> <
|
85
|
+
@subscriber = JTask.get("email_subscribers.json", 50)
|
86
|
+
#=> <JTask::Get "id"="50" "email"="yukihiro@matsumoto.net">
|
77
87
|
|
78
88
|
@subscriber.email
|
79
89
|
#=> "yukihiro@matsumoto.net"
|
@@ -82,80 +92,48 @@ As seen above - calling JTask.get() without a method argument will return **all*
|
|
82
92
|
JTask also comes with a few retrieval methods similar to Active Record. Let's get the **first and last** `n` email subscribers:
|
83
93
|
|
84
94
|
``` ruby
|
85
|
-
JTask.get("email_subscribers", first: 25)
|
86
|
-
#=> [ <
|
95
|
+
JTask.get("email_subscribers.json", first: 25)
|
96
|
+
#=> [ <JTask::Get "id"=1>, <JTask::Get "id"=2>, ..., <JTask::Get "id"=25> ]
|
87
97
|
|
88
|
-
JTask.get("email_subscribers", last: 1)
|
89
|
-
#=> <
|
98
|
+
JTask.get("email_subscribers.json", last: 1)
|
99
|
+
#=> <JTask::Get "id"=365 "email"="goo@goo.gl">
|
90
100
|
```
|
91
101
|
|
92
|
-
|
93
|
-
*Updates
|
102
|
+
#### [JTask.update(filename, id, parameters, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.update() "View full guide")
|
103
|
+
*Updates an existing JSON object with a new set of values.*
|
94
104
|
|
95
105
|
``` ruby
|
96
|
-
JTask.update("ui_settings", 42, {show_ads: "no", background: "grey"})
|
106
|
+
JTask.update("ui_settings.json", 42, {show_ads: "no", background: "grey"})
|
97
107
|
```
|
98
108
|
|
99
109
|
JTask upgrades records gracefully - parameters already existing inside the JSON object will be replaced with the new value, whereas new parameters will be added.
|
100
110
|
|
101
111
|
``` ruby
|
102
112
|
# Original Version
|
103
|
-
<
|
113
|
+
<JTask::Get "id"=42 "show_ads"="yes">
|
104
114
|
|
105
115
|
# Updated Version
|
106
|
-
<
|
116
|
+
<JTask::Get "id"=42 "show_ads"="no" "background"="grey">
|
107
117
|
```
|
108
118
|
|
109
119
|
To completely remove parameters (the entire key-value pair) from objects, refer to the JTask.chop() method below.
|
110
120
|
|
111
|
-
|
121
|
+
#### [JTask.destroy(filename, id, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.destroy() "View full guide")
|
112
122
|
*Removes an entire object from the file.*
|
113
123
|
|
114
|
-
|
115
|
-
|
116
|
-
```
|
117
|
-
|
118
|
-
### [JTask.chop(filename, id, paramter, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.chop() "View full guide")
|
119
|
-
*Removes an entire key-value pair (or parameter) from one or all of the file's objects.*
|
120
|
-
|
121
|
-
``` ruby
|
122
|
-
JTask.chop("users", 4, "session_data")
|
123
|
-
|
124
|
-
JTask.chop("users", :all, "session_data")
|
125
|
-
```
|
124
|
+
#### [JTask.chop(filename, id, paramter, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.chop() "View full guide")
|
125
|
+
*Removes an entire key-value pair from one or all of the file's objects.*
|
126
126
|
|
127
|
-
|
128
|
-
|
129
|
-
``` ruby
|
130
|
-
# Old Version
|
131
|
-
<OpenStruct "id"=4 "user_id"="p18573" "session_data"="34F3jkdf9azfvVak2">
|
132
|
-
|
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")
|
127
|
+
#### [JTask.rename(filename, new, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.rename() "View full guide")
|
140
128
|
*Simply renames the file to something different.*
|
141
129
|
|
142
|
-
|
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")
|
130
|
+
#### [JTask.kill(filename, dir=nil)](https://github.com/adammcarthur/jtask/wiki/JTask.kill() "View full guide")
|
147
131
|
*Completely removes the entire file specified from the system.*
|
148
132
|
|
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
|
-
|
155
133
|
## Share your ideas and contribute
|
156
134
|
|
157
135
|
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).
|
158
136
|
|
159
|
-
To contribute to the project, fork it, send a pull request and I'll review your changes. Check out the
|
137
|
+
To contribute to the project, fork it, send a pull request and I'll review your changes. Check out the [enhancement suggestions page](https://github.com/adammcarthur/jtask/issues?labels=enhancement) to see what still needs to be done.
|
160
138
|
|
161
139
|
\- Adam
|
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.
|
4
|
+
version: 0.3.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-04-
|
11
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -35,8 +35,11 @@ files:
|
|
35
35
|
- jtask.gemspec
|
36
36
|
- lib/jtask.rb
|
37
37
|
- lib/jtask/chop.rb
|
38
|
+
- lib/jtask/config.rb
|
39
|
+
- lib/jtask/convert.rb
|
38
40
|
- lib/jtask/destroy.rb
|
39
41
|
- lib/jtask/get.rb
|
42
|
+
- lib/jtask/helpers.rb
|
40
43
|
- lib/jtask/kill.rb
|
41
44
|
- lib/jtask/rename.rb
|
42
45
|
- lib/jtask/save.rb
|
@@ -52,7 +55,7 @@ post_install_message: |2-
|
|
52
55
|
|
53
56
|
Thanks for installing JTask Beta. Check out the full documentation and contribute at https://github.com/adammcarthur/jtask
|
54
57
|
|
55
|
-
In this release (v0.
|
58
|
+
In this release (v0.3.0 Beta): https://github.com/adammcarthur/jtask/releases/tag/v0.3.0
|
56
59
|
|
57
60
|
- Adam (@adammcarth)
|
58
61
|
-
|