module Rtasklib::Helpers

A collection of stateless, non-end-user facing functions available throughout the library

Public Instance Methods

arbitrary_attr(attr, depth: 1) click to toggle source

Returns part of attribute at a given depth

@param attr [String] @param depth [Integer] @return [Boolean] @api public

# File lib/rtasklib/helpers.rb, line 170
def arbitrary_attr attr, depth: 1
  attr.to_s.split("_")[depth]
end
deep_attr(attr, depth: 2) click to toggle source

Returns all attribute string after given depth

@param attr [String] @param depth [Integer] @return [Boolean] @api public

# File lib/rtasklib/helpers.rb, line 180
def deep_attr attr, depth: 2
  attr.to_s.split("_")[depth..-1].join("_")
end
determine_type(value) click to toggle source

Determine the type that a value should be coerced to Int needs to precede float because ints are also floats Doesn't detect arrays, b/c task stores these as comma separated strings which could just as easily be Strings.… If nothing works it defaults to String.

@param value [Object] anything that needs to be coerced, probably string @return [Axiom::Types::Boolean, Integer, Float, String] @api public

# File lib/rtasklib/helpers.rb, line 216
def determine_type value
  if boolean? value
    return Axiom::Types::Boolean
  elsif integer? value
    return Integer
  elsif float? value
    return Float
  elsif json? value
    return MultiJson
  else
    return String
  end
end
filter(ids: nil, tags: nil, dom: nil) click to toggle source

Converts ids, tags, and dom queries to a single string ready to pass directly to task.

@param ids[Range, Array<String, Range, Fixnum>, String, Fixnum] @param tags[String, Array<String>] @param dom[String, Array<String>] @return [String] a string with ids tags and dom joined by a space @api public

# File lib/rtasklib/helpers.rb, line 49
def filter ids: nil, tags: nil, dom: nil
  id_s = tag_s = dom_s = ""
  id_s   = process_ids(ids)   unless ids.nil?
  tag_s  = process_tags(tags) unless tags.nil?
  dom_s  = process_dom(dom)   unless dom.nil?
  return "#{id_s} #{tag_s} #{dom_s}".strip
end
id_a_to_s(id_a) click to toggle source

Filters should be a list of values Ranges interpreted as ids 1…5 : “1,2,3,4,5” 1..5 : “1,2,3,4” 1 : “1” and joined with “,”

1…5, 8, 9

: “1,2,3,4,5,8,9”

@param id_a [Array<String, Range, Fixnum>] @return [String] @api public

# File lib/rtasklib/helpers.rb, line 68
def id_a_to_s id_a
  id_a.map do |el|
     proc_ids = process_ids(el)
     proc_ids
  end.compact.join(",")
end
id_range_to_s(id_range) click to toggle source

Convert a range to a comma separated strings, e.g. 1..4 -> “1,2,3,4”

@param id_range [Range] @return [Array<String>] @api public

# File lib/rtasklib/helpers.rb, line 97
def id_range_to_s id_range
  id_range.to_a.join(",")
end
pending_or_waiting(use=true) click to toggle source

Returns a “+PENDING or +WAITING” tag string if true, else “”

@param use [Boolean] @return [String] @api public

# File lib/rtasklib/helpers.rb, line 199
def pending_or_waiting use=true
  if use
    "+PENDING or +WAITING"
  else
    ""
  end
end
process_dom(dom) click to toggle source

Process string and array input of the likes of project:Work or description.contains:yolo

@param dom [String, Array<String>, Hash] @api public

# File lib/rtasklib/helpers.rb, line 135
def process_dom dom
  case dom
  when String
    dom
  when Array
    dom.join(" ")
  when Hash
    process_hash_dom(dom)
  end
end
process_hash_dom(dom_hash) click to toggle source

Parse the hash input to a string

@param dom_hash [Hash] @return [String] @api public

# File lib/rtasklib/helpers.rb, line 151
def process_hash_dom dom_hash
  dom_hash.reduce("") { |dom, (k,v)| dom += "#{k.to_s}:#{v} " }.strip
end
process_ids(ids) click to toggle source

Converts arbitrary id input to a task safe string

@param ids[Range, Array<String, Range, Fixnum>, String, Fixnum] @api public

# File lib/rtasklib/helpers.rb, line 79
def process_ids ids
  case ids
  when Range
    return id_range_to_s(ids)
  when Array
    return id_a_to_s(ids)
  when String
    return ids.delete(" ")
  when Fixnum
    return ids
  end
end
process_tag(tag) click to toggle source

Ensures that a tag begins with a + or - or is one of the operational operators `and or xor < <= = != >= > ( )`

@param tag [String] @return [String] @api public

# File lib/rtasklib/helpers.rb, line 120
def process_tag tag
  reserved_symbols = %w{+ - and or xor < <= = != >=  > ( )}

  # convert plain tags to plus tags
  unless tag.start_with?(*reserved_symbols)
    tag = "+#{tag}"
  end
  return tag
end
process_tags(tags) click to toggle source

Convert a tag string or an array of strings to a space separated string

@param tags [String, Array<String>] @api private

# File lib/rtasklib/helpers.rb, line 105
def process_tags tags
  case tags
  when String
    tags.split(" ").map { |t| process_tag t }.join(" ")
  when Array
    tags.map { |t| process_tags t }.join(" ")
  end
end
to_gem_version(raw) click to toggle source

Converts a string of format “1.6.2 (adf342jsd)” to Gem::Version object

@param raw [String] @return [Gem::Version] @api public

# File lib/rtasklib/helpers.rb, line 189
def to_gem_version raw
  std_ver = raw.chomp.gsub(' ','.').delete('(').delete(')')
  Gem::Version.new std_ver
end
uda_attr?(attr) click to toggle source

Is a given taskrc attribute dealing with udas?

@param attr [String] @return [Boolean] @api public

# File lib/rtasklib/helpers.rb, line 160
def uda_attr? attr
  attr.to_s.start_with? "uda"
end
wrap_string(string) click to toggle source

Wrap a string with quotes to make it safe to pass to `task`

@param string [String] @api public

# File lib/rtasklib/helpers.rb, line 37
def wrap_string string
  "\"#{string.to_s}\""
end

Private Instance Methods

boolean?(value) click to toggle source

Can the input be coerced to a boolean without losing information?

@return [Boolean] true if coercible, false if not @api private

# File lib/rtasklib/helpers.rb, line 271
def boolean? value
  ["on", "off", "yes", "no", "false", "true"]
    .include? value.to_s.downcase rescue false
end
float?(value) click to toggle source

Can the input be coerced to a float without losing information?

@return [Boolean] true if coercible, false if not @api private

# File lib/rtasklib/helpers.rb, line 258
def float? value
  begin
    true if Float(value)
  rescue
    false
  end
end
integer?(value) click to toggle source

Can the input be coerced to an integer without losing information?

@return [Boolean] true if coercible, false if not @api private

# File lib/rtasklib/helpers.rb, line 234
def integer? value
  value.to_i.to_s == value rescue false
end
json?(value) click to toggle source

Can the input be coerced to a JSON object without losing information?

@return [Boolean] true if coercible, false if not @api private

# File lib/rtasklib/helpers.rb, line 243
def json? value
  begin
    return false unless value.is_a? String
    MultiJson.load(value)
    true
  rescue MultiJson::ParseError
    false
  end
end