Module: Humidifier::Utils

Defined in:
lib/humidifier/utils.rb,
ext/humidifier/humidifier.c

Overview

Dumps an object to CFN syntax

Class Method Summary collapse

Class Method Details

.enumerable_to_h(enumerable) ⇒ Object

back-supporting ruby 2.0’s lack of Enumerable#to_h



8
9
10
11
12
13
# File 'lib/humidifier/utils.rb', line 8

def enumerable_to_h(enumerable)
  enumerable.each_with_object({}) do |item, result|
    key, value = yield item
    result[key] = value
  end
end

.underscore(str) ⇒ Object

takes a string and returns a downcased version where capitals are now separated by underscores



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/humidifier/humidifier.c', line 45

static VALUE underscore(VALUE self, VALUE str)
{
  if (TYPE(str) == T_NIL) return Qnil;

  char *str_value = rb_string_value_cstr(&str);
  char orig_str[strlen(str_value) + 1];

  strcpy(orig_str, str_value);
  preprocess(orig_str);

  // manually null-terminating the string because on Fedora for strings of length 16 this breaks otherwise
  orig_str[strlen(str_value)] = '\0';

  char new_str[strlen(orig_str) * 2];
  char prev;
  int orig_idx, new_idx;

  for (orig_idx = 0, new_idx = 0; orig_str[orig_idx] != '\0'; orig_idx++) {
    if (orig_idx != 0 && isupper(orig_str[orig_idx])) {
      new_str[new_idx++] = '_';
    }
    new_str[new_idx++] = tolower(orig_str[orig_idx]);
    prev = tolower(orig_str[orig_idx]);
  }

  return rb_str_new(new_str, new_idx);
}

.underscored(names) ⇒ Object

a frozen hash of the given names mapped to their underscored version



16
17
18
# File 'lib/humidifier/utils.rb', line 16

def underscored(names)
  names.each_with_object({}) { |name, map| map[name] = underscore(name).to_sym }.freeze
end