Class: Cheferize::ChefString

Inherits:
Object
  • Object
show all
Defined in:
lib/cheferize/chef_string.rb

Overview

A ChefString provides an additional public method for String. in_same_case_as.

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ ChefString

Returns a new instance of ChefString.



6
7
8
# File 'lib/cheferize/chef_string.rb', line 6

def initialize(str)
  @target = str
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)

proxy any remaining calls to the underlying string.



40
41
42
# File 'lib/cheferize/chef_string.rb', line 40

def method_missing(name, *args, &block)
  @target.send(name, *args, &block)
end

Instance Method Details

#get_case(letter) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/cheferize/chef_string.rb', line 15

def get_case(letter)
  code = letter.codepoints[0]
  if code > 96 && code < 123
    :lower
  elsif code > 64 && code < 91
    :upper
  else
    :non_alpha
  end
end

#in_same_case_as(target) ⇒ Object

return target string, transformed into the same case as the first character of target.



11
12
13
# File 'lib/cheferize/chef_string.rb', line 11

def in_same_case_as(target)
  to_case(@target, get_case(target))
end

#to_case(letter, sym) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/cheferize/chef_string.rb', line 26

def to_case(letter, sym)
  case sym
  when :upper
    letter.upcase
  when :lower
    letter.downcase
  else
    letter
  end
end