Sha256: 88caf9f3e3d401af624188561adc59e95a844c06f434b686cc18967fe5f638c7

Contents?: true

Size: 2 KB

Versions: 53

Compression:

Stored size: 2 KB

Contents

module Intercom
  module Utils
    class << self
      def singularize(str)
        str.gsub(/ies$/, 'y').gsub(/s$/, '')
      end

      def pluralize(str)
        return str.gsub(/y$/, 'ies') if str =~ /y$/
        "#{str}s"
      end

      # the constantize method that exists in rails to allow for ruby 1.9 to get namespaced constants
      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

      def resource_class_to_singular_name(resource_class)
        resource_class.to_s.split('::')[-1].downcase
      end

      def resource_class_to_collection_name(resource_class)
        Utils.pluralize(resource_class_to_singular_name(resource_class))
      end

      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

      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

      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

      def entity_key_from_type(type)
        is_list = type.split('.')[1] == 'list'
        entity_name = type.split('.')[0]
        is_list ?  Utils.pluralize(entity_name) : entity_name
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
intercom-3.5.24 lib/intercom/utils.rb
intercom-3.5.23 lib/intercom/utils.rb
intercom-3.5.22 lib/intercom/utils.rb
intercom-3.5.21 lib/intercom/utils.rb
intercom-3.5.20 lib/intercom/utils.rb
intercom-3.5.19 lib/intercom/utils.rb
intercom-3.5.17 lib/intercom/utils.rb
intercom-3.5.16 lib/intercom/utils.rb
intercom-3.5.15 lib/intercom/utils.rb
intercom-3.5.14 lib/intercom/utils.rb
intercom-3.5.12 lib/intercom/utils.rb
intercom-3.5.11 lib/intercom/utils.rb
intercom-3.5.10 lib/intercom/utils.rb
intercom-3.5.9 lib/intercom/utils.rb
intercom-3.5.8 lib/intercom/utils.rb
intercom-3.5.7 lib/intercom/utils.rb
intercom-3.5.6 lib/intercom/utils.rb
intercom-3.5.5 lib/intercom/utils.rb
intercom-3.5.4 lib/intercom/utils.rb
intercom-3.5.3 lib/intercom/utils.rb