Sha256: 094edadf9a25031359274e472eb8378194b4246a50d5a0a35796e5af08403e68

Contents?: true

Size: 1.29 KB

Versions: 91

Compression:

Stored size: 1.29 KB

Contents

module Recurly
  module Helper
    def camelize underscored_word
      underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
    end

    def classify table_name
      camelize singularize(table_name.to_s.sub(/.*\./, ''))
    end

    def demodulize class_name_in_module
      class_name_in_module.to_s.sub(/^.*::/, '')
    end

    def pluralize word
      word.to_s.sub(/([^s])$/, '\1s')
    end

    def singularize word
      word = word.to_s
      return word if word.end_with?('address')
      return "shipping_address" if word == "shipping_addresses"
      word.sub(/s$/, '').sub(/ie$/, 'y')
    end

    def underscore camel_cased_word
      word = camel_cased_word.to_s.dup
      word.gsub!(/::/, '/')
      word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
      word.tr! "-", "_"
      word.downcase!
      word
    end

    def hash_with_indifferent_read_access base = {}
      indifferent = Hash.new { |hash, key| hash[key.to_s] if key.is_a? Symbol }
      base.each_pair { |key, value| indifferent[key.to_s] = value }
      indifferent
    end

    def stringify_keys! hash
      hash.keys.each do |key|
        stringify_keys! hash[key] if hash[key].is_a? Hash
        hash[key.to_s] = hash.delete key if key.is_a? Symbol
      end
    end

    extend self
  end
end

Version data entries

91 entries across 91 versions & 1 rubygems

Version Path
recurly-2.20.3 lib/recurly/helper.rb
recurly-2.20.2 lib/recurly/helper.rb
recurly-2.20.1 lib/recurly/helper.rb
recurly-2.20.0 lib/recurly/helper.rb
recurly-2.19.13 lib/recurly/helper.rb
recurly-2.19.12 lib/recurly/helper.rb
recurly-2.19.11 lib/recurly/helper.rb
recurly-2.19.10 lib/recurly/helper.rb
recurly-2.19.9 lib/recurly/helper.rb
recurly-2.19.8 lib/recurly/helper.rb
recurly-2.19.7 lib/recurly/helper.rb
recurly-2.19.6 lib/recurly/helper.rb
recurly-2.19.5 lib/recurly/helper.rb
recurly-2.19.4 lib/recurly/helper.rb
recurly-2.19.3 lib/recurly/helper.rb
recurly-2.19.2 lib/recurly/helper.rb
recurly-2.19.1 lib/recurly/helper.rb
recurly-2.19.0 lib/recurly/helper.rb
recurly-2.18.34 lib/recurly/helper.rb
recurly-2.18.33 lib/recurly/helper.rb