Sha256: be7e7c93c2b128a8dfa2b3e4bcc63c19b7b6e290e15a2ccd7d69ecc448e13f1f

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

#
#       ActiveFacts Runtime API.
#       Various additions or patches to Ruby built-in classes, and some global support methods
#
# Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
#

# Define Infinity as a constant, if it's not already defined:
# We use this to define open-ended ranges.
begin
  Object.const_get("Infinity")
rescue
  Infinity = 1.0/0.0
end

class Symbol #:nodoc:
  def to_proc
    Proc.new{|*args| args.shift.__send__(self, *args)}
  end
end

class String #:nodoc:
  # This may be overridden by a version from ActiveSupport. For our purposes, either will work.
  def camelcase(first_letter = :upper)
    if first_letter == :upper
      gsub(/(^|[_\s]+)([A-Za-z])/){ $2.upcase }
    else
      gsub(/([_\s]+)([A-Za-z])/){ $2.upcase }
    end
  end

  def snakecase
    gsub(/([a-z])([A-Z])/,'\1_\2').downcase
  end

  def camelwords
    gsub(/-([a-zA-Z])/){ $1.upcase }.                 # Break and upcase on hyphenated words
      gsub(/([a-z])([A-Z])/,'\1_\2').
      split(/[_\s]+/)
  end
end

class Module #:nodoc:
  def modspace
    space = name[ 0...(name.rindex( '::' ) || 0)]
    space == '' ? Object : eval(space)
  end

  def basename
    name.gsub(/.*::/, '')
  end
end

module ActiveFacts #:nodoc:
  # If the args array ends with a hash, remove it.
  # If the remaining args are fewer than the arg_names,
  # extract values from the hash and append them to args.
  # Return the new args array and the hash.
  # In any case leave the original args unmodified.
  def self.extract_hash_args(arg_names, args)
    if Hash === args[-1]
      arg_hash = args[-1]     # Don't pop args, leave it unmodified
      args = args[0..-2]
      arg_hash = arg_hash.clone if (args.size < arg_names.size)
      while args.size < arg_names.size
        args << arg_hash[n = arg_names[args.size]]
        arg_hash.delete(n)
      end
    else
      arg_hash = {}
    end
    return args, arg_hash
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activefacts-api-0.9.2 lib/activefacts/api/support.rb
activefacts-api-0.9.1 lib/activefacts/api/support.rb
activefacts-api-0.8.12 lib/activefacts/api/support.rb