Class: Hyperion::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperion/util.rb

Class Method Summary collapse

Class Method Details

.bind(name, value) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/hyperion/util.rb', line 43

def bind(name, value)
  old_value = Thread.current[name]
  begin
    Thread.current[name] = value
    yield
  ensure
    Thread.current[name] = old_value
  end
end

.camel_case(str) ⇒ Object



6
7
8
9
# File 'lib/hyperion/util.rb', line 6

def camel_case(str)
  cameled = str.gsub(/[_| |\-][A-Za-z]/) { |a| a[1..-1].upcase } if str
  uncapitalize(cameled)
end

.capitalize(str) ⇒ Object



19
20
21
22
23
# File 'lib/hyperion/util.rb', line 19

def capitalize(str)
  do_to_first(str) do |first_letter|
    first_letter.upcase
  end
end

.class_name(str) ⇒ Object



11
12
13
# File 'lib/hyperion/util.rb', line 11

def class_name(str)
  capitalize(camel_case(str))
end

.do_to_first(str) ⇒ Object



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

def do_to_first(str)
  if str
    first = yield(str[0, 1])
    if str.length > 1
      last = str[1..-1]
      first + last
    else
      first
    end
  end
end

.snake_case(str) ⇒ Object



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

def snake_case(str)
  str.gsub(/([a-z0-9])([A-Z])/, '\1 \2').downcase.gsub(/[ |\-]/, '_') if str
end

.uncapitalize(str) ⇒ Object



25
26
27
28
29
# File 'lib/hyperion/util.rb', line 25

def uncapitalize(str)
  do_to_first(str) do |first_letter|
    first_letter.downcase
  end
end