Class: JTask
- Inherits:
-
Object
- Object
- JTask
- Defined in:
- lib/jtask.rb,
lib/jtask/get.rb,
lib/jtask/get.rb,
lib/jtask/chop.rb,
lib/jtask/kill.rb,
lib/jtask/save.rb,
lib/jtask/config.rb,
lib/jtask/rename.rb,
lib/jtask/update.rb,
lib/jtask/convert.rb,
lib/jtask/destroy.rb,
lib/jtask/helpers.rb
Overview
JTask.rename() Simply renames the file to something different.
Eg: JTask.rename(“orders.json”, “0r3erz.json”) #=> true
See wiki guide for more usage examples… github.com/adammcarthur/jtask/wiki/JTask.rename()
Defined Under Namespace
Modules: Convert, Helpers Classes: Configuration, Get
Class Method Summary collapse
- .chop(filename, id, parameter, dir = nil) ⇒ Object
-
.configuration ⇒ Object
Allow options to be set under the JTask namespace.
- .configure {|configuration| ... } ⇒ Object
- .destroy(filename, id, dir = nil) ⇒ Object
- .get(filename, method = nil, dir = nil) ⇒ Object
- .kill(filename, dir = nil) ⇒ Object
- .rename(filename, new_filename, dir = nil) ⇒ Object
- .save(filename, parameters, dir = nil) ⇒ Object
- .update(filename, id, parameters, dir = nil) ⇒ Object
Class Method Details
.chop(filename, id, parameter, dir = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jtask/chop.rb', line 14 def self.chop(filename, id, parameter, dir=nil) # Set the directory dir = JTask::Helpers.set_directory(dir) # Parse the file original_file = File.read(File.join(dir, filename)) objects = JSON.parse(original_file) # Quality control - ensure paramter is a string unless parameter.is_a?(String) raise SyntaxError, "[JTask] The chop() method can only remove one parameter at a time - only a string can be used to specify the parameter." end if id.is_a?(Integer) if objects["#{id}"] # Find object with the id and delete the requested parameter removed_version = objects["#{id}"].tap{ |x| x.delete(parameter) } insert = objects insert["#{id}"] = removed_version else raise NameError, "[JTask] The id #{method} could not be found in the file \"#{dir + filename}\". The file has not been changed." end elsif id == :all # Parameter must be removed from all objects objects.each do |k, v| # Remove parameter from every hash objects["#{k}"] = objects["#{k}"].tap{ |x| x.delete(parameter) } end insert = objects else raise NameError, "[JTask] Incorrect id method used. A single id (integer) can be specified, or the symbol \":all\" to remove the parameter from every object." end # Re-write the file with the new version. File.write(File.join(dir, filename), insert.to_json) return true end |
.configuration ⇒ Object
Allow options to be set under the JTask namespace.
14 15 16 |
# File 'lib/jtask/config.rb', line 14 def self.configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
18 19 20 21 |
# File 'lib/jtask/config.rb', line 18 def self.configure # Update Configuration with new values if config the block is given. yield(configuration) if block_given? end |
.destroy(filename, id, dir = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/jtask/destroy.rb', line 14 def self.destroy(filename, id, dir=nil) # Set the directory dir = JTask::Helpers.set_directory(dir) # Parse the file original_file = File.read(File.join(dir, filename)) objects = JSON.parse(original_file) if objects["#{id}"] # Delete the object with id n from the objects hash insert = objects.tap{ |x| x.delete("#{id}") } else # the id doesn't exist in the file raise NameError, "[JTask] An id of #{id} does not exsist in the file specified at \"#{dir + filename}\". Nothing has been deleted." end # Re-write the file with the new version. File.write(File.join(dir, filename), insert.to_json) return true end |
.get(filename, method = nil, dir = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/jtask/get.rb', line 36 def self.get(filename, method=nil, dir=nil) # Set the directory dir = JTask::Helpers.set_directory(dir) # Parse the file original_file = File.read(File.join(dir, filename)) objects = JSON.parse(original_file) # Work out which retrieval method is wanted. # An integer indicates we want to find a single record by id. # A hash indicates the first/last method has been used. # Check for integer if method.is_a?(Integer) # Our method will be the id - lets alias it: id = method if objects["#{id}"] output = JTask::Get.new({ "id" => id.to_i }.merge(objects["#{id}"])) else # id supplied doesn't exist raise NameError, "[JTask] The id #{method} could not be found in the file \"#{dir + filename}\"." end else # Method could be either blank, invalid or a "first/last" hash. begin # Treat method as a hash # Assemble hashes of the required records, as specified by the user. if method[:first] required_records = Hash[(objects.to_a).first(method[:first].to_i)] elsif method[:last] required_records = Hash[(objects.to_a).last(method[:last].to_i).reverse] # wow! end # Rescue to prevent '[]' nil class error if method is empty rescue if method == nil # We want all the records since no get method is supplied required_records = objects else # Unrecognisable value used as the method, crash immediatly. raise SyntaxError, "[JTask] Invalid value given for the get method." end end # Loop through each required record and # assemble each key-value into the open structure output. # Map all openstructs to an array. output = required_records.map { |id, record| JTask::Get.new({ "id" => id.to_i }.merge(record)) } end return output end |
.kill(filename, dir = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/jtask/kill.rb', line 13 def self.kill(filename, dir=nil) # Set the directory dir = JTask::Helpers.set_directory(dir) # Delete the file File.delete(File.join(dir, filename)) return true end |
.rename(filename, new_filename, dir = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/jtask/rename.rb', line 11 def self.rename(filename, new_filename, dir=nil) # Set the directory dir = JTask::Helpers.set_directory(dir) # Rename the file File.rename(File.join(dir, filename), File.join(dir, new_filename)) return true end |
.save(filename, parameters, dir = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jtask/save.rb', line 14 def self.save(filename, parameters, dir=nil) # Set the directory dir = JTask::Helpers.set_directory(dir) # Check if the file already exists unless File.exists?(File.join(dir, filename)) # Create the file since it doesn’t exist, and setup up for JSON. File.open(File.join(dir, filename), "w+") { |file| file.write("{}") } end # Validate haash supplied for parameters unless parameters.is_a?(Hash) raise SyntaxError, "[JTask] Invalid value supplied to the parameters variable. Ensure your parameters are in the symbol hash form, eg - {name: \"Adam\", city: \"Melbourne\"}" end original_file = File.read(File.join(dir, filename)) objects = JSON.parse(original_file) # Set the id of the new object if (objects.to_a).first # Add 1 to last object id nextid = (objects.to_a).last[0].to_i + 1 else # No current objects exist yet, so set the id to 1 nextid = 1 end # Remove last character "}" from file. insert = File.read(File.join(dir, filename))[0..-2] insert << "," if (objects.to_a).first # add a comma if at least one object already exists insert << "\"#{nextid.to_s}\":" insert << parameters.to_json # Extra } used to replace the one we took out originally insert << "}" # Re-write the file with the new version. File.write(File.join(dir, filename), insert) return true end |
.update(filename, id, parameters, dir = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/jtask/update.rb', line 14 def self.update(filename, id, parameters, dir=nil) # Set the directory dir = JTask::Helpers.set_directory(dir) # Parse the file original_file = File.read(File.join(dir, filename)) objects = JSON.parse(original_file) insert = objects parameters.each do |k, v| # Update (or add) each parameter insert["#{id}"][k.to_s] = v end # Re-write the file with the new version. File.write(File.join(dir, filename), insert.to_json) return true end |