Module: Tweetwine::Support
Defined Under Namespace
Modules: Helper
Instance Method Summary collapse
- #blank?(var) ⇒ Boolean
- #find_hash_path(hash, path) ⇒ Object
- #humanize_time_diff(from, to) ⇒ Object
- #parse_int_gt(value, default, min, name_for_error) ⇒ Object
- #presence(var) ⇒ Object
- #present?(var) ⇒ Boolean
- #str_gsub_by_group(str, regexp) ⇒ Object
- #stringify_hash_keys(hash) ⇒ Object
- #symbolize_hash_keys(hash) ⇒ Object
- #unescape_html(str) ⇒ Object
Instance Method Details
#blank?(var) ⇒ Boolean
10 11 12 |
# File 'lib/tweetwine/support.rb', line 10 def blank?(var) var.nil? || var.empty? end |
#find_hash_path(hash, path) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/tweetwine/support.rb', line 87 def find_hash_path(hash, path) return nil if hash.nil? path = [path] unless path.is_a? Array path.inject(hash) do |result, key| return hash.default if key.nil? || result.nil? result[key] end end |
#humanize_time_diff(from, to) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tweetwine/support.rb', line 26 def humanize_time_diff(from, to) from = Time.parse(from.to_s) unless from.is_a? Time to = Time.parse(to.to_s) unless to.is_a? Time difference = (to - from).to_i.abs value, unit = case difference when 0..59 then [difference, "sec"] when 60..3599 then [(difference/60.0).round, "min"] when 3600..86399 then [(difference/3600.0).round, "hour"] else [(difference/86400.0).round, "day"] end [value, Helper.pluralize_unit(value, unit)] end |
#parse_int_gt(value, default, min, name_for_error) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tweetwine/support.rb', line 50 def parse_int_gt(value, default, min, name_for_error) if value value = value.to_i if value >= min value else raise ArgumentError, "Invalid #{name_for_error} -- must be greater than or equal to #{min}" end else default end end |
#presence(var) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/tweetwine/support.rb', line 18 def presence(var) if present? var block_given? ? yield(var) : var else nil end end |
#present?(var) ⇒ Boolean
14 15 16 |
# File 'lib/tweetwine/support.rb', line 14 def present?(var) !blank?(var) end |
#str_gsub_by_group(str, regexp) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tweetwine/support.rb', line 63 def str_gsub_by_group(str, regexp) dup_str = str.dup str_pos, dup_pos = 0, 0 while str_pos < str.size && (match_data = regexp.match(str[str_pos..-1])) matching_group_indexes = Helper.indexes_of_filled_matches(match_data) matching_group_indexes.each do |i| replacement = (yield match_data[i]).to_s dup_str[dup_pos + match_data.begin(i), match_data[i].size] = replacement replacement_delta = replacement.size - match_data[i].size dup_pos += replacement_delta end skip_delta = match_data.end(0) str_pos += skip_delta dup_pos += skip_delta end dup_str end |
#stringify_hash_keys(hash) ⇒ Object
42 43 44 |
# File 'lib/tweetwine/support.rb', line 42 def stringify_hash_keys(hash) Helper.recursive_copy_hash(hash) { |key, value| [key.to_s, value] } end |
#symbolize_hash_keys(hash) ⇒ Object
46 47 48 |
# File 'lib/tweetwine/support.rb', line 46 def symbolize_hash_keys(hash) Helper.recursive_copy_hash(hash) { |key, value| [key.to_sym, value] } end |
#unescape_html(str) ⇒ Object
83 84 85 |
# File 'lib/tweetwine/support.rb', line 83 def unescape_html(str) CGI.unescapeHTML(str.gsub(' ', ' ')) end |