Module: Intercom::Utils
- Defined in:
- lib/intercom/utils.rb
Class Method Summary collapse
- .camelize(snake_cased_word) ⇒ Object
-
.constantize(camel_cased_word) ⇒ Object
the constantize method that exists in rails to allow for ruby 1.9 to get namespaced constants.
- .constantize_resource_name(resource_name) ⇒ Object
- .constantize_singular_resource_name(resource_name) ⇒ Object
- .define_lightweight_class(class_name) ⇒ Object
- .entity_key_from_type(type) ⇒ Object
- .maybe_underscore_name(resource_name) ⇒ Object
- .pluralize(str) ⇒ Object
- .resource_class_to_collection_name(resource_class) ⇒ Object
- .resource_class_to_singular_name(resource_class) ⇒ Object
- .singularize(str) ⇒ Object
Class Method Details
.camelize(snake_cased_word) ⇒ Object
28 29 30 |
# File 'lib/intercom/utils.rb', line 28 def camelize(snake_cased_word) snake_cased_word.split(/_/).map(&:capitalize).join end |
.constantize(camel_cased_word) ⇒ Object
the constantize method that exists in rails to allow for ruby 1.9 to get namespaced constants
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/intercom/utils.rb', line 17 def constantize(camel_cased_word) names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end |
.constantize_resource_name(resource_name) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/intercom/utils.rb', line 46 def constantize_resource_name(resource_name) class_name = camelize Utils.singularize(resource_name.capitalize) define_lightweight_class(class_name) unless Intercom.const_defined?(class_name, false) namespaced_class_name = "Intercom::#{class_name}" constantize namespaced_class_name end |
.constantize_singular_resource_name(resource_name) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/intercom/utils.rb', line 53 def constantize_singular_resource_name(resource_name) class_name = resource_name.split('_').map(&:capitalize).join define_lightweight_class(class_name) unless Intercom.const_defined?(class_name, false) namespaced_class_name = "Intercom::#{class_name}" constantize namespaced_class_name end |
.define_lightweight_class(class_name) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/intercom/utils.rb', line 60 def define_lightweight_class(class_name) new_class_definition = Class.new(Object) do include Traits::ApiResource end Intercom.const_set(class_name, new_class_definition) end |
.entity_key_from_type(type) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/intercom/utils.rb', line 67 def entity_key_from_type(type) return 'data' if type == 'list' is_list = type.split('.')[1] == 'list' entity_name = type.split('.')[0] return Utils.pluralize(entity_name) if entity_name == 'event' is_list ? Utils.pluralize(entity_name) : entity_name end |
.maybe_underscore_name(resource_name) ⇒ Object
38 39 40 |
# File 'lib/intercom/utils.rb', line 38 def maybe_underscore_name(resource_name) resource_name.gsub!(/(.)([A-Z])/, '\1_\2') || resource_name end |
.pluralize(str) ⇒ Object
10 11 12 13 14 |
# File 'lib/intercom/utils.rb', line 10 def pluralize(str) return str.gsub(/y$/, 'ies') if str =~ /y$/ "#{str}s" end |
.resource_class_to_collection_name(resource_class) ⇒ Object
42 43 44 |
# File 'lib/intercom/utils.rb', line 42 def resource_class_to_collection_name(resource_class) Utils.pluralize(resource_class_to_singular_name(resource_class)) end |
.resource_class_to_singular_name(resource_class) ⇒ Object
32 33 34 35 36 |
# File 'lib/intercom/utils.rb', line 32 def resource_class_to_singular_name(resource_class) resource_name = resource_class.to_s.split('::')[-1] resource_name = maybe_underscore_name(resource_name) resource_name.downcase end |
.singularize(str) ⇒ Object
6 7 8 |
# File 'lib/intercom/utils.rb', line 6 def singularize(str) str.gsub(/ies$/, 'y').gsub(/s$/, '') end |