Skip to content

Commit

Permalink
memoize keys
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Sep 22, 2016
1 parent 2f4e734 commit 4049ec5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
case_transform (0.1)
case_transform (0.2)
activesupport

GEM
Expand Down
26 changes: 21 additions & 5 deletions lib/case_transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,22 @@
require 'case_transform/version'

module CaseTransform
module_function
class << self
def camel_cache
@camel_cache ||= {}
end

def camel_lower_cache
@camel_lower_cache ||= {}
end

def dash_cache
@dash_cache ||= {}
end

def underscore_cache
@underscore_cache ||= {}
end

# Transforms values to UpperCamelCase or PascalCase.
#
Expand All @@ -16,7 +31,7 @@ def camel(value)
when Array then value.map { |item| camel(item) }
when Hash then value.deep_transform_keys! { |key| camel(key) }
when Symbol then camel(value.to_s).to_sym
when String then value.underscore.camelize
when String then camel_cache[value] ||= value.underscore.camelize
else value
end
end
Expand All @@ -30,7 +45,7 @@ def camel_lower(value)
when Array then value.map { |item| camel_lower(item) }
when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
when Symbol then camel_lower(value.to_s).to_sym
when String then value.underscore.camelize(:lower)
when String then camel_lower_cache[value] ||= value.underscore.camelize(:lower)
else value
end
end
Expand All @@ -45,7 +60,7 @@ def dash(value)
when Array then value.map { |item| dash(item) }
when Hash then value.deep_transform_keys! { |key| dash(key) }
when Symbol then dash(value.to_s).to_sym
when String then value.underscore.dasherize
when String then dash_cache[value] ||= value.underscore.dasherize
else value
end
end
Expand All @@ -60,7 +75,7 @@ def underscore(value)
when Array then value.map { |item| underscore(item) }
when Hash then value.deep_transform_keys! { |key| underscore(key) }
when Symbol then underscore(value.to_s).to_sym
when String then value.underscore
when String then underscore_cache[value] ||= value.underscore
else value
end
end
Expand All @@ -69,4 +84,5 @@ def underscore(value)
def unaltered(value)
value
end
end
end
2 changes: 1 addition & 1 deletion lib/case_transform/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
module CaseTransform
VERSION = '0.1'.freeze
VERSION = '0.2'.freeze
end

0 comments on commit 4049ec5

Please sign in to comment.