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

#arbitrary_attr(attr, depth: 1) ⇒ Boolean

Returns part of attribute at a given depth

Parameters:

  • attr (String)
  • depth (Integer) (defaults to: 1)

Returns:

  • (Boolean)


147
148
149
# File 'lib/rtasklib/helpers.rb', line 147

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

#deep_attr(attr, depth: 2) ⇒ Boolean

Returns all attribute string after given depth

Parameters:

  • attr (String)
  • depth (Integer) (defaults to: 2)

Returns:

  • (Boolean)


157
158
159
# File 'lib/rtasklib/helpers.rb', line 157

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

#determine_type(value) ⇒ Axiom::Types::Boolean, ...

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. TODO: JSON parse

Parameters:

  • value (Object)

    anything that needs to be coerced, probably string

Returns:

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


194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rtasklib/helpers.rb', line 194

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) ⇒ String

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

Parameters:

  • ids (Range, Array<String, Range, Fixnum>, String, Fixnum) (defaults to: nil)
  • tags (String, Array<String>) (defaults to: nil)
  • dom (String, Array<String>) (defaults to: nil)

Returns:

  • (String)

    a string with ids tags and dom joined by a space



28
29
30
31
32
33
34
# File 'lib/rtasklib/helpers.rb', line 28

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) ⇒ String

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:

  • (String)


47
48
49
50
51
52
# File 'lib/rtasklib/helpers.rb', line 47

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) ⇒ Array<String>

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

Parameters:

  • id_range (Range)

Returns:

  • (Array<String>)


76
77
78
# File 'lib/rtasklib/helpers.rb', line 76

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

#pending_or_waiting(use = true) ⇒ String

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

Parameters:

  • use (Boolean) (defaults to: true)

Returns:

  • (String)


176
177
178
179
180
181
182
# File 'lib/rtasklib/helpers.rb', line 176

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

#process_dom(dom) ⇒ Object

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

Parameters:

  • dom (String, Array<String>, Hash)


112
113
114
115
116
117
118
119
120
121
# File 'lib/rtasklib/helpers.rb', line 112

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) ⇒ String

Parse the hash input to a string

Parameters:

  • dom_hash (Hash)

Returns:

  • (String)


128
129
130
# File 'lib/rtasklib/helpers.rb', line 128

def process_hash_dom dom_hash
  dom_hash.reduce("") { |dom, (k,v)| dom += "#{k.to_s}:#{v} " }.strip
end

#process_ids(ids) ⇒ Object

Converts arbitrary id input to a task safe string

Parameters:

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


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rtasklib/helpers.rb', line 58

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) ⇒ String

Ensures that a tag begins with a + or -

Returns:

  • (String)


97
98
99
100
101
102
103
104
105
# File 'lib/rtasklib/helpers.rb', line 97

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) ⇒ Object

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:

  • tags (String, Array<String>)


84
85
86
87
88
89
90
91
# File 'lib/rtasklib/helpers.rb', line 84

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) ⇒ Gem::Version

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

Parameters:

  • raw (String)

Returns:

  • (Gem::Version)


166
167
168
169
# File 'lib/rtasklib/helpers.rb', line 166

def to_gem_version raw
  std_ver = raw.chomp.gsub(' ','.').delete('(').delete(')')
  Gem::Version.new std_ver
end

#uda_attr?(attr) ⇒ Boolean

Is a given taskrc attribute dealing with udas?

Parameters:

  • attr (String)

Returns:

  • (Boolean)


137
138
139
# File 'lib/rtasklib/helpers.rb', line 137

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

#wrap_string(string) ⇒ Object

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

Parameters:

  • string (String)


16
17
18
# File 'lib/rtasklib/helpers.rb', line 16

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