Skip to content

Commit

Permalink
Add custom initialisms, replacy janky uncombine method
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson committed Aug 29, 2019
1 parent dd48387 commit bf1966c
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 33 deletions.
12 changes: 12 additions & 0 deletions compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# generation.
ENV['TZ'] = 'UTC'

require 'active_support/inflector'
require 'api/compiler'
require 'google/logger'
require 'optparse'
Expand Down Expand Up @@ -86,6 +87,17 @@
end.parse!
# rubocop:enable Metrics/BlockLength

# We use ActiveSupport Inflections to perform common string operations like
# going from camelCase/PascalCase -> snake_case -> Title Case.
# In order to not break the world, they've frozen the list of inflections the
# library uses by default.
# Particularly for initialisms, it may need a little help to generate great
# code.
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'TPU'
inflect.acronym 'VPC'
end

raise 'Cannot use -p/--products and -a/--all simultaneously' \
if products_to_generate && all_products
raise 'Either -p/--products OR -a/--all must be present' \
Expand Down
8 changes: 0 additions & 8 deletions google/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ def underscore
Google::StringUtils.underscore(self)
end

def camelize(style = :lower)
Google::StringUtils.camelize(self, style)
end

def uncombine
Google::StringUtils.uncombine(self)
end

def symbolize
Google::StringUtils.symbolize(self)
end
Expand Down
18 changes: 0 additions & 18 deletions google/string_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@
module Google
# Helper class to process and mutate strings.
class StringUtils
# Converts string from underscore to camel case
def self.camelize(source, style = :lower)
camelized = source.gsub(/_(.)/, &:upcase).delete('_')
case style
when :lower
camelized[0] = camelized[0].downcase
when :upper
camelized[0] = camelized[0].upcase
else
raise "Unknown camel case style: #{style}"
end
camelized
end

# Converts string from camel case to underscore
def self.underscore(source)
Expand All @@ -38,11 +25,6 @@ def self.underscore(source)
.downcase
end

# Add spaces before every capitalized word except first.
def self.uncombine(source)
source.gsub(/(?=[A-Z])/, ' ').strip
end

# rubocop:disable Style/SafeNavigation # support Ruby < 2.3.0
def self.symbolize(key)
key.to_sym unless key.nil?
Expand Down
4 changes: 1 addition & 3 deletions provider/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ def format2regex(format)
# Capitalize the first letter of a property name.
# E.g. "creationTimestamp" becomes "CreationTimestamp".
def titlelize_property(property)
p = property.name.clone
p[0] = p[0].capitalize
p
property.name.camelize(:upper)
end

private
Expand Down
2 changes: 1 addition & 1 deletion templates/ansible/documentation.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ DOCUMENTATION = '''

<% if example -%>
EXAMPLES = '''
<% res_readable_name = object.name.uncombine -%>
<% res_readable_name = object.name.titleize -%>
<% if example.dependencies -%>
<% example.dependencies.each do |depend| -%>
<%= lines(depend.build_test('present', object, false), 1) -%>
Expand Down
2 changes: 1 addition & 1 deletion templates/ansible/integration_test.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<% end # if example.dependencies -%>
<%= lines(example.task.build_test('absent', object, false)) -%>
#----------------------------------------------------------
<% resource_name = object.name.uncombine.downcase -%>
<% resource_name = object.name.titleize.downcase -%>
<%= lines(example.task.build_test('present', object, false)) -%>
register: result
<% if object.readonly -%>
Expand Down
2 changes: 1 addition & 1 deletion templates/terraform/examples/base_configs/tutorial.md.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% autogen_exception -%>
<%= "# #{example.name.camelize(:upper).uncombine} - Terraform" %>
<%= "# #{example.name.camelize(:upper).titleize} - Terraform" %>

## Setup

Expand Down
2 changes: 1 addition & 1 deletion templates/terraform/resource.html.markdown.erb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ To get more information about <%= object.name -%>, see:
</a>
</div>
<%- end -%>
## Example Usage - <%= example.name.camelize(:upper).uncombine %>
## Example Usage - <%= example.name.camelize(:upper).titleize %>
<%= example.config_documentation -%>
Expand Down

0 comments on commit bf1966c

Please sign in to comment.