Module: Codgen::AutoStyle

Defined in:
lib/codgen/auto_style.rb

Class Method Summary collapse

Class Method Details

.add_property_group(json_object, key, value, translate, explicit_postfix) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/codgen/auto_style.rb', line 40

def self.add_property_group(json_object, key, value, translate, explicit_postfix)
  should_translate_val = value != nil && value.is_a?(String) && key.index('@')
  is_plural = key.index('?plural')

  new_key = translate.call(key)
  new_value = should_translate_val ? translate.call(value) : value
  explicit_key = new_key + explicit_postfix

  requantified = pluralize_singularize(new_key, explicit_key, new_value, is_plural, should_translate_val)

  try_add_prop(json_object, new_key, new_value)
  try_add_prop(json_object, explicit_key, new_value)
  try_add_prop(json_object, requantified.short_key, requantified.value)
  try_add_prop(json_object, requantified.explicit_key, requantified.value)
end

.pluralize_singularize(short_key, explicit_key, value, is_plural, should_translate_value) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/codgen/auto_style.rb', line 56

def self.pluralize_singularize(short_key, explicit_key, value, is_plural, should_translate_value)
  return_obj = OpenStruct.new
  if is_plural
    return_obj.short_key = short_key.singularize
    return_obj.explicit_key = explicit_key.singularize
    return_obj.value = should_translate_value ? value.singularize : value
  else
    return_obj.short_key = short_key.pluralize
    return_obj.explicit_key = explicit_key.pluralize
    return_obj.value = should_translate_value ? value.pluralize : value
  end
  return_obj
end

.style(json_object) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/codgen/auto_style.rb', line 6

def self.style(json_object)
  if json_object.is_a?(Array)
    json_object.each { |child| style(child) }
  end

  add_props = Hash.new
  if json_object.is_a?(Hash)
    json_object.each do |key, value|
      if value.is_a?(Hash) || value.is_a?(Array)
        style(value)
        next
      end

      if key.index(/[@$ #]/)
        add_props.store(key, value)
      end
    end
  end

  add_props.each do |key, value|
    add_property_group(json_object, key, value, AutoStyle.method(:to_camel), '!camelCase')
    add_property_group(json_object, key, value, AutoStyle.method(:to_cap_camel), '!CapCamel')
    add_property_group(json_object, key, value, AutoStyle.method(:to_underscore), '!underscored')
    add_property_group(json_object, key, value, AutoStyle.method(:to_camel), '!CAPS_UNDERSCORE')
  end
end

.style_state_variable(input_string) ⇒ Object



34
35
36
# File 'lib/codgen/auto_style.rb', line 34

def self.style_state_variable(input_string)
  [to_camel(input_string), to_cap_camel(input_string), to_underscore(input_string), to_underscore(input_string)]
end

.to_allcaps_underscore(input_string) ⇒ Object



105
106
107
108
109
# File 'lib/codgen/auto_style.rb', line 105

def self.to_allcaps_underscore(input_string)
  universal = to_universal(input_string)
  output = universal.join('_')
  output.upcase
end

.to_camel(input_string) ⇒ Object



91
92
93
94
95
# File 'lib/codgen/auto_style.rb', line 91

def self.to_camel(input_string)
  universal = to_universal(input_string)
  universal[1...universal.count].each {|word| word[0] = word[0].upcase}
  universal.join('')
end

.to_cap_camel(input_string) ⇒ Object



84
85
86
87
88
# File 'lib/codgen/auto_style.rb', line 84

def self.to_cap_camel(input_string)
  universal = to_universal(input_string)
  universal.each {|word| word[0] = word[0].upcase}
  universal.join('')
end

.to_underscore(input_string) ⇒ Object



98
99
100
101
102
# File 'lib/codgen/auto_style.rb', line 98

def self.to_underscore(input_string)
  universal = to_universal(input_string)
  output = universal.join('_')
  output.downcase
end

.to_universal(input_string) ⇒ Object



78
79
80
81
# File 'lib/codgen/auto_style.rb', line 78

def self.to_universal(input_string)
  output_string = input_string.tr('$?@', '')
  output_string.split(' ')
end

.try_add_prop(json_object, key, value) ⇒ Object



71
72
73
74
75
# File 'lib/codgen/auto_style.rb', line 71

def self.try_add_prop(json_object, key, value)
  unless json_object.has_key?(key)
    json_object.store(key, value)
  end
end