Module: Rtasklib::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/rtasklib/helpers.rb

Overview

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

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) arbitrary_attr(attr, depth: 1)

Returns part of attribute at a given depth

Parameters:

  • attr (String)
  • depth (Integer)

Returns:

  • (Boolean)


170
171
172
# File 'lib/rtasklib/helpers.rb', line 170

def arbitrary_attr attr, depth: 1
  attr.to_s.split("_")[depth]
end

- (Boolean) deep_attr(attr, depth: 2)

Returns all attribute string after given depth

Parameters:

  • attr (String)
  • depth (Integer)

Returns:

  • (Boolean)


180
181
182
# File 'lib/rtasklib/helpers.rb', line 180

def deep_attr attr, depth: 2
  attr.to_s.split("_")[depth..-1].join("_")
end

- (Axiom::Types::Boolean, ...) determine_type(value)

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.

Parameters:

  • value (Object)

    anything that needs to be coerced, probably string

Returns:

  • (Axiom::Types::Boolean, Integer, Float, String)


216
217
218
219
220
221
222
223
224
225
226
227
228
# 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

- (String) filter(ids: nil, tags: nil, dom: nil)

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

Parameters:

Returns:

  • (String)

    a string with ids tags and dom joined by a space



49
50
51
52
53
54
55
# 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

- (String) id_a_to_s(id_a)

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”

Parameters:

  • id_a (Array<String, Range, Fixnum>)

Returns:



68
69
70
71
72
73
# 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

- (Array<String>) id_range_to_s(id_range)

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

Parameters:

  • id_range (Range)

Returns:



97
98
99
# File 'lib/rtasklib/helpers.rb', line 97

def id_range_to_s id_range
  id_range.to_a.join(",")
end

- (String) pending_or_waiting(use = true)

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

Parameters:

  • use (Boolean) (defaults to: true)

Returns:



199
200
201
202
203
204
205
# File 'lib/rtasklib/helpers.rb', line 199

def pending_or_waiting use=true
  if use
    "+PENDING or +WAITING"
  else
    ""
  end
end

- (Object) process_dom(dom)

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

Parameters:



135
136
137
138
139
140
141
142
143
144
# 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

- (String) process_hash_dom(dom_hash)

Parse the hash input to a string

Parameters:

  • dom_hash (Hash)

Returns:



151
152
153
# 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

- (Object) process_ids(ids)

Converts arbitrary id input to a task safe string

Parameters:



79
80
81
82
83
84
85
86
87
88
89
90
# 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

- (String) process_tag(tag)

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

Parameters:

Returns:



120
121
122
123
124
125
126
127
128
# 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

- (Object) process_tags(tags)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:



105
106
107
108
109
110
111
112
# 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

- (Gem::Version) to_gem_version(raw)

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

Parameters:

Returns:

  • (Gem::Version)


189
190
191
192
# 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

- (Boolean) uda_attr?(attr)

Is a given taskrc attribute dealing with udas?

Parameters:

Returns:

  • (Boolean)


160
161
162
# File 'lib/rtasklib/helpers.rb', line 160

def uda_attr? attr
  attr.to_s.start_with? "uda"
end

- (Object) wrap_string(string)

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

Parameters:



37
38
39
# File 'lib/rtasklib/helpers.rb', line 37

def wrap_string string
  "\"#{string.to_s}\""
end