Class: Lazylead::Opts
- Inherits:
-
Object
- Object
- Lazylead::Opts
- Extended by:
- Forwardable
- Defined in:
- lib/lazylead/opts.rb
Overview
Default options for all lazylead tasks.
- Author
-
Yurii Dubinka ([email protected])
- Copyright
-
Copyright © 2019-2020 Yurii Dubinka
- License
-
MIT
Instance Method Summary collapse
- #blank?(key) ⇒ Boolean
- #construct(field, delim: ",") ⇒ Object
-
#decrypt(key, sid) ⇒ Object
Decrypt particular option using cryptography salt.
-
#find(key, default = nil) ⇒ Object
Get the value by key.
-
#initialize(origin = {}) ⇒ Opts
constructor
A new instance of Opts.
-
#jira_defaults ⇒ Object
Default Jira options to use during search for all Jira-based tasks.
-
#jira_fields ⇒ Object
Default fields which to fetch within the Jira issue.
- #merge(args) ⇒ Object
- #msg_attachments(delim = ",") ⇒ Object
-
#msg_body(template = "template") ⇒ Object
Construct html document from template and binds.
- #msg_cc(delim = ",") ⇒ Object
- #msg_from(delim = ",") ⇒ Object
- #msg_to(delim = ",") ⇒ Object
-
#numeric?(key) ⇒ Boolean
Ensure that particular key from options is a positive numer Opts.new(“key” => “1”).numeric? “key” => true Opts.new(“key” => “0”).numeric? “key” => false Opts.new(“key” => “.”).numeric? “key” => false Opts.new(“key” => “nil”).numeric? “key” => false.
-
#slice(key, delim) ⇒ Object
Split text value by delimiter, trim all spaces and reject blank items.
-
#sliced(delim, *keys) ⇒ Object
Find the option by key and split by delimiter Opts.new(“key” => “a,b”).sliced(“,”, “key”) => [a, b] Opts.new(key: “a,b”).sliced(“,”, :key) => [a, b] Opts.new(key: “a,b”).sliced(“,”, “key”, :key) => [a, b] Opts.new(key: “”).sliced “,”, :key) => [].
- #to_h ⇒ Object
-
#trim(arr) ⇒ Object
Trim all spaces and reject blank items in array.
Constructor Details
#initialize(origin = {}) ⇒ Opts
Returns a new instance of Opts.
39 40 41 |
# File 'lib/lazylead/opts.rb', line 39 def initialize(origin = {}) @origin = origin end |
Instance Method Details
#blank?(key) ⇒ Boolean
55 56 57 |
# File 'lib/lazylead/opts.rb', line 55 def blank?(key) to_h[key].nil? || @origin[key].blank? end |
#construct(field, delim: ",") ⇒ Object
143 144 145 |
# File 'lib/lazylead/opts.rb', line 143 def construct(field, delim: ",") slice(field, delim).map(&:constantize).map(&:new) end |
#decrypt(key, sid) ⇒ Object
Decrypt particular option using cryptography salt
81 82 83 84 85 86 |
# File 'lib/lazylead/opts.rb', line 81 def decrypt(key, sid) text = to_h[key] return text if text.blank? || text.nil? return Salt.new(sid).decrypt(text) if ENV.key? sid text end |
#find(key, default = nil) ⇒ Object
Get the value by key
153 154 155 156 |
# File 'lib/lazylead/opts.rb', line 153 def find(key, default = nil) return default if key.nil? to_h[key.to_s] || to_h[key.to_sym] || default end |
#jira_defaults ⇒ Object
Default Jira options to use during search for all Jira-based tasks.
64 65 66 67 68 69 70 |
# File 'lib/lazylead/opts.rb', line 64 def jira_defaults { max_results: fetch("max_results", 50), fields: jira_fields, limit: to_h["limit"] } end |
#jira_fields ⇒ Object
Default fields which to fetch within the Jira issue
73 74 75 |
# File 'lib/lazylead/opts.rb', line 73 def jira_fields to_h.fetch("fields", "").split(",").map(&:to_sym) end |
#merge(args) ⇒ Object
88 89 90 91 |
# File 'lib/lazylead/opts.rb', line 88 def merge(args) return self unless args.is_a? Hash Opts.new @origin.merge(args) end |
#msg_attachments(delim = ",") ⇒ Object
113 114 115 |
# File 'lib/lazylead/opts.rb', line 113 def (delim = ",") sliced(delim, :attachments, "attachments").select { |f| File.file? f } end |
#msg_body(template = "template") ⇒ Object
Construct html document from template and binds.
94 95 96 97 98 99 |
# File 'lib/lazylead/opts.rb', line 94 def msg_body(template = "template") Email.new( to_h[template], to_h.merge(version: Lazylead::VERSION) ).render end |
#msg_cc(delim = ",") ⇒ Object
105 106 107 |
# File 'lib/lazylead/opts.rb', line 105 def msg_cc(delim = ",") sliced delim, :cc, "cc" end |
#msg_from(delim = ",") ⇒ Object
109 110 111 |
# File 'lib/lazylead/opts.rb', line 109 def msg_from(delim = ",") sliced delim, :from, "from" end |
#msg_to(delim = ",") ⇒ Object
101 102 103 |
# File 'lib/lazylead/opts.rb', line 101 def msg_to(delim = ",") sliced delim, :to, "to" end |
#numeric?(key) ⇒ Boolean
139 140 141 |
# File 'lib/lazylead/opts.rb', line 139 def numeric?(key) to_h[key].to_i.positive? end |
#slice(key, delim) ⇒ Object
Split text value by delimiter, trim all spaces and reject blank items
44 45 46 47 |
# File 'lib/lazylead/opts.rb', line 44 def slice(key, delim) return [] unless to_h.key? key trim to_h[key].split(delim) end |
#sliced(delim, *keys) ⇒ Object
Find the option by key and split by delimiter
Opts.new("key" => "a,b").sliced(",", "key") => [a, b]
Opts.new(key: "a,b").sliced(",", :key) => [a, b]
Opts.new(key: "a,b").sliced(",", "key", :key) => [a, b]
Opts.new(key: "").sliced ",", :key) => []
124 125 126 127 128 129 130 131 132 |
# File 'lib/lazylead/opts.rb', line 124 def sliced(delim, *keys) return [] if keys.empty? key = keys.detect { |k| key? k } val = to_h[key] return [] if val.nil? || val.blank? return val if val.is_a? Array return [val] unless val.include? delim slice key, delim end |
#to_h ⇒ Object
59 60 61 |
# File 'lib/lazylead/opts.rb', line 59 def to_h @origin end |
#trim(arr) ⇒ Object
Trim all spaces and reject blank items in array
50 51 52 53 |
# File 'lib/lazylead/opts.rb', line 50 def trim(arr) return [] if arr.nil? arr.map(&:chomp).map(&:strip).reject(&:blank?) end |