Class: Rtasklib::Taskrc
- Inherits:
-
Object
- Object
- Rtasklib::Taskrc
- Defined in:
- lib/rtasklib/taskrc.rb
Overview
A class that wraps a single Virtus domain model with a number of creation and manipulation methods
Intialize with either a hash of attribute value pairs or a Pathname to the raw taskrc file to read.
Instance Attribute Summary (collapse)
-
- (Models::TaskrcModel) config
A custom Virtus domain model.
Instance Method Summary (collapse)
-
- (Object) get_model_attr_value(attr)
Gets the current value of a given attr in the config object.
-
- (Taskrc) initialize(rc, type = :array)
constructor
Generate a dynamic Virtus model, with the attributes defined by the input.
-
- (Array<String>) model_to_rc
Serialize all attrs of the model to the taskrc format.
-
- (String) model_to_s
Serialize all attrs model back to the taskrc format and reduce them to a string that can be passed directly to Execute.
-
- (Array<String>) part_of_model_to_rc(*attrs)
Serialize the given attrs model back to the taskrc format.
-
- (String) part_of_model_to_s(*attrs)
Serialize the given attrs model back to the taskrc format and reduce them to a string that can be passed directly to Execute.
-
- (undefined) set_model_attr_value(attr, value)
Modifies the value of a given attr in the config object.
Constructor Details
- (Taskrc) initialize(rc, type = :array)
Generate a dynamic Virtus model, with the attributes defined by the input
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rtasklib/taskrc.rb', line 28 def initialize rc, type=:array @config = Models::TaskrcModel.new().extend(Virtus.model) case type when :array mappable_to_model(rc) when :hash hash_to_model(rc) when :path if path_exist?(rc) mappable_to_model(File.open(rc).readlines) else raise RuntimeError.new("rc path does not exist on the file system") end else raise TypeError.new("no implicit conversion to Hash, String, or Pathname") end end |
Instance Attribute Details
- (Models::TaskrcModel) config
Returns a custom Virtus domain model
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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/rtasklib/taskrc.rb', line 19 class Taskrc attr_accessor :config # Generate a dynamic Virtus model, with the attributes defined by the input # # @param rc [Hash, Pathname] either a hash of attribute value pairs # or a Pathname to the raw taskrc file. # @raise [TypeError] if rc is not of type Hash, String, or Pathname # @raise [RuntimeError] if rc is a path and does not exist on the fs def initialize rc, type=:array @config = Models::TaskrcModel.new().extend(Virtus.model) case type when :array mappable_to_model(rc) when :hash hash_to_model(rc) when :path if path_exist?(rc) mappable_to_model(File.open(rc).readlines) else raise RuntimeError.new("rc path does not exist on the file system") end else raise TypeError.new("no implicit conversion to Hash, String, or Pathname") end end # Turn a hash of attribute => value pairs into a TaskrcModel object. # There can be only one TaskrcModel object per Taskrc, it's saved to the # instance variable `config` # # @param taskrc_hash [Hash{Symbol=>String}] # @return [Models::TaskrcModel] the instance variable config # @api private def hash_to_model taskrc_hash taskrc_hash.each do |attr, value| add_model_attr(attr, value) set_model_attr_value(attr, value) end config end private :hash_to_model # Converts a .taskrc file path into a Hash that can be converted into a # TaskrcModel object # # @param rc_file [String,Pathname] a valid pathname to a .taskrc file # @return [Models::TaskrcModel] the instance variable config # @api private def mappable_to_model rc_file rc_file.map! { |l| line_to_tuple(l) }.compact! taskrc = Hash[rc_file] hash_to_model(taskrc) end private :mappable_to_model # Converts a line of the form "json.array=on" to [ :json_array, true ] # # @param line [String] a line from a .taskrc file # @return [Array<Symbol, Object>, nil] a valid line returns an array of # length 2, invalid input returns nil # @api private def line_to_tuple line line = line.chomp.split('=', 2) if line.size == 2 and not line.include? "#" attr = get_hash_attr_from_rc line[0] return [ attr.to_sym, line[1] ] else return nil end end private :line_to_tuple # Serialize the given attrs model back to the taskrc format # # @param attrs [Array] a splat of attributes # @return [Array<String>] an array of CLI formatted strings # @api public def part_of_model_to_rc *attrs attrs.map do |attr| value = get_model_attr_value attr hash_attr = get_rc_attr_from_hash attr.to_s attr = "rc.#{hash_attr}=#{value}" end end # Serialize all attrs of the model to the taskrc format # # @return [Array<String>] an array of CLI formatted strings # @api public def model_to_rc part_of_model_to_rc(*config.attributes.keys) end # Serialize the given attrs model back to the taskrc format and reduce them # to a string that can be passed directly to Execute # # @param attrs [Array] a splat of attributes # @return [String] a CLI formatted string # @api public def part_of_model_to_s *attrs part_of_model_to_rc(*attrs).join(" ") end # Serialize all attrs model back to the taskrc format and reduce them # to a string that can be passed directly to Execute # # @return [String] a CLI formatted string # @api public def model_to_s model_to_rc().join(" ") end # Dynamically add a Virtus attr, detect Boolean, Integer, and Float types # based on the value, otherwise just treat it like a string. # Modifies the config instance variable # TODO: May also be able to detect arrays # # @param attr [#to_sym] the name for the attr, e.g. "json_array" # @param value [String] the value of the attr, e.g. "yes" # @return [undefined] # @api private def add_model_attr attr, value config.attribute(attr.to_sym, Helpers.determine_type(value)) end private :add_model_attr # Modifies the value of a given attr in the config object # # @param attr [#to_s] the name for the attr, e.g. "json_array" # @param value [String] the value of the attr, e.g. "yes" # @return [undefined] # @api public def set_model_attr_value attr, value config.send("#{attr}=".to_sym, value) end # Gets the current value of a given attr in the config object # # @param attr [#to_s] the name for the attr, e.g. "json_array" # @return [Object] the type varies depending on the type attr # @api public def get_model_attr_value attr config.send("#{attr.to_s}".to_sym) end # Convert dot notation to Symbol safe underscores # # @param attr [String] the name for the attr, e.g. "json_array" # @return [String] # @api private def get_hash_attr_from_rc attr return attr.gsub(".", "_") end private :get_hash_attr_from_rc # Convert Symbol safe underscores to dot notation # # @param attr [String] the name for the attr, e.g. "json_array" # @return [String] # @api private def get_rc_attr_from_hash attr return attr.gsub("_", ".") end private :get_rc_attr_from_hash # Check whether a given object is a path and it exists on the file system # # @param path [Object] # @return [Boolean] # @api private def path_exist? path if path.is_a? Pathname return path.exist? elsif path.is_a? String return Pathname.new(path).exist? else return false end end private :path_exist? end |
Instance Method Details
- (Object) get_model_attr_value(attr)
Gets the current value of a given attr in the config object
163 164 165 |
# File 'lib/rtasklib/taskrc.rb', line 163 def get_model_attr_value attr config.send("#{attr.to_s}".to_sym) end |
- (Array<String>) model_to_rc
Serialize all attrs of the model to the taskrc format
111 112 113 |
# File 'lib/rtasklib/taskrc.rb', line 111 def model_to_rc part_of_model_to_rc(*config.attributes.keys) end |
- (String) model_to_s
Serialize all attrs model back to the taskrc format and reduce them to a string that can be passed directly to Execute
130 131 132 |
# File 'lib/rtasklib/taskrc.rb', line 130 def model_to_s model_to_rc().join(" ") end |
- (Array<String>) part_of_model_to_rc(*attrs)
Serialize the given attrs model back to the taskrc format
99 100 101 102 103 104 105 |
# File 'lib/rtasklib/taskrc.rb', line 99 def part_of_model_to_rc *attrs attrs.map do |attr| value = get_model_attr_value attr hash_attr = get_rc_attr_from_hash attr.to_s attr = "rc.#{hash_attr}=#{value}" end end |
- (String) part_of_model_to_s(*attrs)
Serialize the given attrs model back to the taskrc format and reduce them to a string that can be passed directly to Execute
121 122 123 |
# File 'lib/rtasklib/taskrc.rb', line 121 def part_of_model_to_s *attrs part_of_model_to_rc(*attrs).join(" ") end |
- (undefined) set_model_attr_value(attr, value)
Modifies the value of a given attr in the config object
154 155 156 |
# File 'lib/rtasklib/taskrc.rb', line 154 def set_model_attr_value attr, value config.send("#{attr}=".to_sym, value) end |