Sha256: fba3cb306f9af176f4b7ab992172c17c87b3fc5d45d29bc8b20c25b9f1e116ee

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

class ::String
  # These methods should not be used in new code
  # Instead use https://github.com/ecraft/ecraft.uxfactory.server/blob/master/documentation/upgrading-from-facets.md

  # require 'active_support'
  # require 'active_support/core_ext/string/inflections'
  #
  # 'active_support_string'.camelize # result: "ActiveSupportString"
  def to_camelcase
    separators = ['_', '\s']
    str = dup
    separators.each do |s|
      str = str.gsub(/(?:#{s}+)([a-z])/) { Regexp.last_match(1).upcase }
    end
    str = str.gsub(/(\A|\s)([a-z])/) { Regexp.last_match(1) + Regexp.last_match(2).upcase }
    str
  end

  # require 'active_support'
  # require 'active_support/core_ext/string/inflections'
  #
  # 'ActiveSupportString'.underscore # result: "active_support_string"
  def to_snake_case
    gsub(/\B[A-Z]/, '_\&').downcase
  end

  # require 'active_support'
  # require 'active_support/core_ext/string/inflections'
  # 'Foo::Bar'.constantize # => Foo::Bar
  def to_class
    chain = split '::'
    klass = Kernel
    chain.each do |klass_string|
      klass = klass.const_get klass_string
    end
    klass.is_a?(Class) ? klass : nil
  rescue NameError
    nil
  end

  def to_bool
    !self !~ /^(true|t|yes|y|1)$/i
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ecraft-extensions-0.3.1 lib/ecraft/extensions/string.rb
ecraft-extensions-0.3.0 lib/ecraft/extensions/string.rb
ecraft-extensions-0.2.3 lib/ecraft/extensions/string.rb
ecraft-extensions-0.2.2 lib/ecraft/extensions/string.rb
ecraft-extensions-0.2.1 lib/ecraft/extensions/string.rb