Module: Boffin::Utils
- Defined in:
- lib/boffin/utils.rb
Overview
A collection of utility methods that are used throughout the library
Constant Summary collapse
- SECONDS_IN_HOUR =
The number of seconds in an hour
3600
- SECONDS_IN_DAY =
Number of seconds in a day
24 * SECONDS_IN_HOUR
- SECONDS_IN_MONTH =
Number of seconds in a month
30 * SECONDS_IN_DAY
- SECONDS_IN_UNIT =
Number of seconds for a single value of each unit
{ :hours => SECONDS_IN_HOUR, :days => SECONDS_IN_DAY, :months => SECONDS_IN_MONTH }
Class Method Summary collapse
-
.blank?(obj) ⇒ true, false
true
if the provided object is blank, examples of blank objects are:[]
,{}
,nil
,false
,''
. -
.extract_time_unit(hsh) ⇒ Array
Pulls time interval information from a hash of options.
- .object_as_identifier(obj, opts = {}) ⇒ String
-
.object_as_key(obj) ⇒ String
A string that can be used as part of a Redis key.
- .object_as_member(obj) ⇒ String
-
.object_as_namespace(obj) ⇒ String
Returns a string that can be used as a namespace in Redis keys.
-
.object_as_uid(obj) ⇒ String
A string that can be used as a member in Keyspace#hits.
-
.respond_to_id?(obj) ⇒ true, false
true
if the provided object responds to :id, other than it's internal object identifierfalse
if the object does not respond to :id. -
.time_ago(time, unit) ⇒ Time
The time in the past offset by the specified amount.
-
.time_ago_range(upto, unit) ⇒ Array<Time>
An array of times in the calculated range.
-
.underscore(thing) ⇒ String
The underscored version of
#to_s
on thing. -
.uniquenesses_as_uid(*aspects) ⇒ String
Generates a unique set member based off the first object in the provided array that is not
nil
.
Class Method Details
.blank?(obj) ⇒ true, false
Returns true
if the provided object is blank, examples of blank objects are:
[]
, {}
, nil
, false
, ''
.
44 45 46 |
# File 'lib/boffin/utils.rb', line 44 def blank?(obj) obj.respond_to?(:empty?) ? obj.empty? : !obj end |
.extract_time_unit(hsh) ⇒ Array
Pulls time interval information from a hash of options.
74 75 76 77 78 79 80 81 82 |
# File 'lib/boffin/utils.rb', line 74 def extract_time_unit(hsh) case when hsh.key?(:hours) then [:hours, hsh[:hours]] when hsh.key?(:days) then [:days, hsh[:days]] when hsh.key?(:months) then [:months, hsh[:months]] else raise ArgumentError, 'no time unit exists in the hash provided' end end |
.object_as_identifier(obj, opts = {}) ⇒ String
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/boffin/utils.rb', line 152 def object_as_identifier(obj, opts = {}) if obj.respond_to?(:as_member) || respond_to_id?(obj) ''.tap do |s| s << "#{underscore(obj.class)}:" if opts[:namespace] s << (obj.respond_to?(:as_member) ? obj.as_member : obj.id).to_s end else opts[:encode] ? [obj.to_s].pack("m0").chomp : obj.to_s end end |
.object_as_key(obj) ⇒ String
Returns A string that can be used as part of a Redis key.
182 183 184 |
# File 'lib/boffin/utils.rb', line 182 def object_as_key(obj) object_as_identifier(obj, :encode => true) end |
.object_as_member(obj) ⇒ String
168 169 170 |
# File 'lib/boffin/utils.rb', line 168 def object_as_member(obj) object_as_identifier(obj) end |
.object_as_namespace(obj) ⇒ String
Returns a string that can be used as a namespace in Redis keys
135 136 137 138 139 140 141 142 |
# File 'lib/boffin/utils.rb', line 135 def object_as_namespace(obj) case obj when String, Symbol obj.to_s else underscore(obj) end end |
.object_as_uid(obj) ⇒ String
Returns A string that can be used as a member in Keyspace#hits.
175 176 177 |
# File 'lib/boffin/utils.rb', line 175 def object_as_uid(obj) object_as_identifier(obj, :namespace => true) end |
.respond_to_id?(obj) ⇒ true, false
Returns true
if the provided object responds to :id, other than it's
internal object identifier
false
if the object does not respond to :id.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/boffin/utils.rb', line 53 def respond_to_id?(obj) # NOTE: this feels like a hack. I'm sure there is a more elegant way # to determine whether the :id method is the built in Object#id but # I can't think of it if RUBY_VERSION < "1.9" obj.respond_to?(:id) and obj.id != obj.object_id else obj.respond_to?(:id) end end |
.time_ago(time, unit) ⇒ Time
Returns The time in the past offset by the specified amount.
93 94 95 96 |
# File 'lib/boffin/utils.rb', line 93 def time_ago(time, unit) unit, unit_value = *extract_time_unit(unit) time - (unit_value * SECONDS_IN_UNIT[unit]) end |
.time_ago_range(upto, unit) ⇒ Array<Time>
Returns An array of times in the calculated range.
107 108 109 110 111 112 113 114 115 |
# File 'lib/boffin/utils.rb', line 107 def time_ago_range(upto, unit) unit, size = *extract_time_unit(unit) ago = time_ago(upto, unit => (size - 1)) max, count, times = upto.to_i, ago.to_i, [] begin times << Time.at(count) end while (count += SECONDS_IN_UNIT[unit]) <= max times end |
.underscore(thing) ⇒ String
Originally pulled from ActiveSupport::Inflector
Returns The underscored version of #to_s
on thing.
30 31 32 33 34 35 36 37 38 |
# File 'lib/boffin/utils.rb', line 30 def underscore(thing) thing.to_s.dup.tap do |word| word.gsub!(/::/, '_') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!('-', '_') word.downcase! end end |
.uniquenesses_as_uid(*aspects) ⇒ String
Generates a unique set member based off the first object in the provided
array that is not nil
. If the array is empty or only contains nil
element then NIL_SESSION_MEMBER is returned.
124 125 126 127 128 129 130 |
# File 'lib/boffin/utils.rb', line 124 def uniquenesses_as_uid(*aspects) if (obj = aspects.flatten.reject { |u| blank?(u) }.first) object_as_uid(obj) else NIL_SESSION_MEMBER end end |