Module: Intercom::Utils
- Defined in:
- lib/intercom/utils.rb
Class Method Summary collapse
-
.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
.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
42 43 44 45 46 47 |
# File 'lib/intercom/utils.rb', line 42 def constantize_resource_name(resource_name) class_name = 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
49 50 51 52 53 54 |
# File 'lib/intercom/utils.rb', line 49 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
56 57 58 59 60 61 |
# File 'lib/intercom/utils.rb', line 56 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
63 64 65 66 67 68 69 |
# File 'lib/intercom/utils.rb', line 63 def entity_key_from_type(type) return 'data' if type == 'list' is_list = type.split('.')[1] == 'list' entity_name = type.split('.')[0] is_list ? Utils.pluralize(entity_name) : entity_name end |
.maybe_underscore_name(resource_name) ⇒ Object
34 35 36 |
# File 'lib/intercom/utils.rb', line 34 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
38 39 40 |
# File 'lib/intercom/utils.rb', line 38 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
28 29 30 31 32 |
# File 'lib/intercom/utils.rb', line 28 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 |