Module: Fiveruns::Dash::Util

Defined in:
lib/fiveruns/dash/util.rb

Overview

Utility methods, largely extracted from ActiveSupport

Class Method Summary collapse

Class Method Details

.blank?(obj) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/fiveruns/dash/util.rb', line 27

def self.blank?(obj)
  obj.respond_to?(:empty?) ? obj.empty? : !self
end

.chain(mod, target, feature) {|aliased_target, punctuation| ... } ⇒ Object

ActiveSupport’s alias_method_chain (but from outside the module) Note: We’d love to use something other than AMC, but it’s the

most consistent wrapper we know of that doesn't require
cooperation from the target.

Yields:

  • (aliased_target, punctuation)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fiveruns/dash/util.rb', line 46

def self.chain(mod, target, feature)
  # Strip out punctuation on predicates or bang methods since
  # e.g. target?_without_feature is not a valid method name.
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
  yield(aliased_target, punctuation) if block_given?

  with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}",
                                "#{aliased_target}_without_#{feature}#{punctuation}"

  mod.send(:alias_method, without_method, target)
  mod.send(:alias_method, target, with_method)

  case
  when mod.public_method_defined?(without_method)
    mod.instance_eval %(public :#{target})
  when mod.protected_method_defined?(without_method)
    mod.instance_eval %(protected :#{target})
  when mod.private_method_defined?(without_method)
    mod.instance_eval %(private :#{target})  
  end
end

.constantize(str) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/fiveruns/dash/util.rb', line 31

def self.constantize(str)
  names = str.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

.demodulize(str) ⇒ Object



6
7
8
# File 'lib/fiveruns/dash/util.rb', line 6

def self.demodulize(str)
  str.sub(/.*::/, '')
end

.shortname(str) ⇒ Object



10
11
12
# File 'lib/fiveruns/dash/util.rb', line 10

def self.shortname(str)
  underscore(demodulize(str))
end

.titleize(str) ⇒ Object

Note: Doesn’t do any fancy Inflector-style modifications



15
16
17
# File 'lib/fiveruns/dash/util.rb', line 15

def self.titleize(str)
  underscore(str.to_s).split(/[_]+/).map { |s| s.capitalize }.join(' ')
end

.underscore(str) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/fiveruns/dash/util.rb', line 19

def self.underscore(str)
  str.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

.version_infoObject



68
69
70
71
72
73
74
# File 'lib/fiveruns/dash/util.rb', line 68

def self.version_info
  if defined?(Gem)
    "with gems #{Gem.loaded_specs.values.find_all {|spec| spec.name =~ /fiveruns-dash/ }.map {|spec| "#{spec.name}-#{spec.version}"}.inspect}"
  else
    Fiveruns::Dash::Version::STRING
  end
end