Module: Katello::Util::Support
- Defined in:
- app/lib/katello/util/support.rb
Class Method Summary collapse
-
.active_record_retry(retries = 3) ⇒ Object
Used for retrying active record transactions when race conditions could cause RecordNotUnique exceptions.
-
.array_with_total(a = []) ⇒ Object
We need this so that we can return empty search results on an invalid query Basically this is a empty array with a total method.
- .deep_copy(object) ⇒ Object
-
.diff_hash_params(rule, params) ⇒ Object
Given a rules hash in the format {<attrib_name> => …} the method will match the params attributes to provided rule and return a diff.
- .scrub(params, &block_to_match) ⇒ Object
-
.stringify(col) ⇒ Object
Helper method to just convert a collection of objects to their string representation (mostly to be used internally).
- .time ⇒ Object
Class Method Details
.active_record_retry(retries = 3) ⇒ Object
Used for retrying active record transactions when race conditions could cause
RecordNotUnique exceptions
77 78 79 80 81 82 83 84 85 86 |
# File 'app/lib/katello/util/support.rb', line 77 def self.active_record_retry(retries = 3) yield rescue ActiveRecord::RecordNotUnique => e retries -= 1 if retries == 0 raise e else retry end end |
.array_with_total(a = []) ⇒ Object
We need this so that we can return empty search results on an invalid query Basically this is a empty array with a total method.
92 93 94 95 96 97 |
# File 'app/lib/katello/util/support.rb', line 92 def self.array_with_total(a = []) def a.total size end a end |
.deep_copy(object) ⇒ Object
4 5 6 |
# File 'app/lib/katello/util/support.rb', line 4 def self.deep_copy(object) Marshal.load(Marshal.dump(object)) end |
.diff_hash_params(rule, params) ⇒ Object
Given a rules hash in the format {<attrib_name> => …} the method will match the params attributes to provided rule and return a diff. Here are some of the examples rule -> => [[:name, :version, :min_version, :max_version]] will match -> => [{:name = > “boo”, :version => “2.0”,
{:name = > "Foo", :min_version => "2.0"}]}
rule -> => [[:id]], :date_range => [:start, :end],
:errata_type => {, :severity => {}}
will match -> {:units => [{:id => 100}],
:date_range => => "05/14/2011"}
Note of caution this merely shows differences in the structure of params vs rules. It doesnt validate anything. Look at SerializedParamsValidator method for its uses.
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 |
# File 'app/lib/katello/util/support.rb', line 47 def self.diff_hash_params(rule, params) params = params.with_indifferent_access if rule.is_a?(Array) return stringify(params.keys) - stringify(rule) end rule = rule.with_indifferent_access diff_data = rule.keys.collect do |k| if params[k] if (params[k].is_a?(Array)) && (rule[k].first.is_a?(Array)) diffs = params[k].collect { |pk| diff_hash_params(rule[k].first, pk) }.flatten diffs elsif params[k].is_a?(Hash) keys = stringify(params[k].keys) - stringify(rule[k]) if keys.empty? nil else {k => keys} end end end end diff_data = diff_data.compact.flatten return diff_data unless diff_data.nil? || diff_data.empty? stringify(params.keys) - stringify(rule.keys) end |
.scrub(params, &block_to_match) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'app/lib/katello/util/support.rb', line 14 def self.scrub(params, &block_to_match) params.keys.each do |key| if params[key].is_a?(Hash) scrub(params[key], &block_to_match) elsif block_to_match.call(key, params[key]) params[key] = "[FILTERED]" end end params end |
.stringify(col) ⇒ Object
Helper method to just convert a collection of objects to their string representation (mostly to be used internally)
28 29 30 |
# File 'app/lib/katello/util/support.rb', line 28 def self.stringify(col) col.collect { |c| c.to_s } end |
.time ⇒ Object
8 9 10 11 12 |
# File 'app/lib/katello/util/support.rb', line 8 def self.time a = Time.now yield Time.now - a end |