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

.underscore(str) ⇒ Object

separated by underscores



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
72
73
74
# File 'ext/humidifier/humidifier.c', line 47

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



5
6
7
# File 'lib/humidifier/utils.rb', line 5

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