Skip to content

Commit

Permalink
add cyg_utils src
Browse files Browse the repository at this point in the history
  • Loading branch information
FLemon committed Dec 10, 2024
1 parent 1a30fe2 commit 6cc4b5d
Show file tree
Hide file tree
Showing 27 changed files with 6,915 additions and 0 deletions.
97 changes: 97 additions & 0 deletions lib/hanami/cyg_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true

require "pathname"

# Hanami - The web, with simplicity
#
# @since 0.1.0
module Hanami
require "hanami/cyg_utils/version"
require "hanami/cyg_utils/file_list"

# Ruby core extentions and Hanami utilities
#
# @since 0.1.0
module CygUtils
# @since 0.3.1
# @api private
HANAMI_JRUBY = "java"

# @since 0.3.1
# @api private
HANAMI_RUBINIUS = "rbx"

# Checks if the current VM is JRuby
#
# @return [TrueClass,FalseClass] info whether the VM is JRuby or not
#
# @since 0.3.1
# @api private
def self.jruby?
RUBY_PLATFORM == HANAMI_JRUBY
end

# Checks if the current VM is Rubinius
#
# @return [TrueClass,FalseClass] info whether the VM is Rubinius or not
#
# @since 0.3.1
# @api private
def self.rubinius?
RUBY_ENGINE == HANAMI_RUBINIUS
end

# Recursively requires Ruby files under the given directory.
#
# If the directory is relative, it implies it's the path from current directory.
# If the directory is absolute, it uses as it is.
#
# It respects file separator of the current operating system.
# A pattern like <tt>"path/to/files"</tt> will work both on *NIX and Windows machines.
#
# @param directory [String, Pathname] the directory
#
# @since 0.9.0
def self.require!(directory)
for_each_file_in(directory) { |file| require_relative(file) }
end

# Recursively reloads Ruby files under the given directory.
#
# If the directory is relative, it implies it's the path from current directory.
# If the directory is absolute, it uses as it is.
#
# It respects file separator of the current operating system.
# A pattern like <tt>"path/to/files"</tt> will work both on *NIX and Windows machines.
#
# @param directory [String, Pathname] the directory
#
# @since 1.0.0
# @api private
def self.reload!(directory)
for_each_file_in(directory) { |file| load(file) }
end

# Recursively scans through the given directory and yields the given block
# for each Ruby source file.
#
# If the directory is relative, it implies it's the path from current directory.
# If the directory is absolute, it uses as it is.
#
# It respects file separator of the current operating system.
# A pattern like <tt>"path/to/files"</tt> will work both on *NIX and Windows machines.
#
# @param directory [String, Pathname] the directory
# @param blk [Proc] the block to yield
#
# @since 1.0.0
# @api private
def self.for_each_file_in(directory, &blk)
directory = directory.to_s.gsub(%r{(\/|\\)}, File::SEPARATOR)
directory = Pathname.new(Dir.pwd).join(directory).to_s
directory = File.join(directory, "**", "*.rb") unless directory =~ /(\*\*)/

FileList[directory].each(&blk)
end
end
end
144 changes: 144 additions & 0 deletions lib/hanami/cyg_utils/basic_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# frozen_string_literal: true

module Hanami
module CygUtils
# BasicObject
#
# @since 0.3.5
class BasicObject < ::BasicObject
# Lookups constants at the top-level namespace, if they are missing in the
# current context.
#
# @param name [Symbol] the constant name
#
# @return [Object, Module] the constant
#
# @raise [NameError] if the constant cannot be found
#
# @since 1.3.4
# @api private
#
# @see https://ruby-doc.org/core/Module.html#method-i-const_missing
def self.const_missing(name)
::Object.const_get(name)
end

# Returns the class for debugging purposes.
#
# @since 0.3.5
#
# @see http://ruby-doc.org/core/Object.html#method-i-class
def class
(class << self; self; end).superclass
end

# Bare minimum inspect for debugging purposes.
#
# @return [String] the inspect string
#
# @since 0.3.5
#
# @see http://ruby-doc.org/core/Object.html#method-i-inspect
#
# rubocop:disable Style/FormatStringToken
def inspect
"#<#{self.class}:#{'0x0000%x' % (__id__ << 1)}#{__inspect}>"
end
# rubocop:enable Style/FormatStringToken

# @!macro [attach] instance_of?(class)
#
# Determines if self is an instance of given class or module
#
# @param class [Class,Module] the class of module to verify
#
# @return [TrueClass,FalseClass] the result of the check
#
# @raise [TypeError] if the given argument is not of the expected types
#
# @since 1.3.2
#
# @see http://ruby-doc.org/core/Object.html#method-i-instance_of-3F
define_method :instance_of?, ::Object.instance_method(:instance_of?)

# @!macro [attach] is_a?(class)
#
# Determines if self is of the type of the object class or module
#
# @param class [Class,Module] the class of module to verify
#
# @return [TrueClass,FalseClass] the result of the check
#
# @raise [TypeError] if the given argument is not of the expected types
#
# @since 1.3.2
#
# @see http://ruby-doc.org/core/Object.html#method-i-is_a-3F
define_method :is_a?, ::Object.instance_method(:is_a?)

# @!macro [attach] kind_of?(class)
#
# Determines if self is of the kind of the object class or module
#
# @param class [Class,Module] the class of module to verify
#
# @return [TrueClass,FalseClass] the result of the check
#
# @raise [TypeError] if the given argument is not of the expected types
#
# @since 1.3.2
#
# @see http://ruby-doc.org/core/Object.html#method-i-kind_of-3F
define_method :kind_of?, ::Object.instance_method(:kind_of?)

# Alias for __id__
#
# @return [Fixnum] the object id
#
# @since 0.9.0
#
# @see http://ruby-doc.org/core/Object.html#method-i-object_id
def object_id
__id__
end

# Interface for pp
#
# @param printer [PP] the Pretty Printable printer
# @return [String] the pretty-printable inspection of the object
#
# @since 0.9.0
#
# @see https://ruby-doc.org/stdlib/libdoc/pp/rdoc/PP.html
def pretty_print(printer)
printer.text(inspect)
end

# Returns true if responds to the given method.
#
# @return [TrueClass,FalseClass] the result of the check
#
# @since 0.3.5
#
# @see http://ruby-doc.org/core-2.2.1/Object.html#method-i-respond_to-3F
def respond_to?(method_name, include_all = false)
respond_to_missing?(method_name, include_all)
end

private

# Must be overridden by descendants
#
# @since 0.3.5
# @api private
def respond_to_missing?(_method_name, _include_all)
::Kernel.raise ::NotImplementedError
end

# @since 0.3.5
# @api private
def __inspect
end
end
end
end
72 changes: 72 additions & 0 deletions lib/hanami/cyg_utils/blank.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

module Hanami
module CygUtils
# Checks for blank
#
# @since 0.8.0
# @api private
class Blank
# Matcher for blank strings
#
# @since 0.8.0
# @api private
STRING_MATCHER = /\A[[:space:]]*\z/.freeze

# Checks if object is blank
#
# @example Basic Usage
# require 'hanami/cyg_utils/blank'
#
# Hanami::CygUtils::Blank.blank?(Hanami::CygUtils::String.new('')) # => true
# Hanami::CygUtils::Blank.blank?(' ') # => true
# Hanami::CygUtils::Blank.blank?(nil) # => true
# Hanami::CygUtils::Blank.blank?(Hanami::CygUtils::Hash.new({})) # => true
# Hanami::CygUtils::Blank.blank?(true) # => false
# Hanami::CygUtils::Blank.blank?(1) # => false
#
# @param object the argument
#
# @return [TrueClass,FalseClass] info, whether object is blank
#
# @since 0.8.0
# @api private
def self.blank?(object)
case object
when String, ::String
STRING_MATCHER === object # rubocop:disable Style/CaseEquality
when Hash, ::Hash, ::Array
object.empty?
when TrueClass, Numeric
false
when FalseClass, NilClass
true
else
object.respond_to?(:empty?) ? object.empty? : !self
end
end

# Checks if object is filled
#
# @example Basic Usage
# require 'hanami/cyg_utils/blank'
#
# Hanami::CygUtils::Blank.filled?(true) # => true
# Hanami::CygUtils::Blank.filled?(1) # => true
# Hanami::CygUtils::Blank.filled?(Hanami::CygUtils::String.new('')) # => false
# Hanami::CygUtils::Blank.filled?(' ') # => false
# Hanami::CygUtils::Blank.filled?(nil) # => false
# Hanami::CygUtils::Blank.filled?(Hanami::CygUtils::Hash.new({})) # => false
#
# @param object the argument
#
# @return [TrueClass,FalseClass] whether the object is filled
#
# @since 1.0.0
# @api private
def self.filled?(object)
!blank?(object)
end
end
end
end
Loading

0 comments on commit 6cc4b5d

Please sign in to comment.