Sha256: 7c82ab33a45fad91d2d01ab99d296f224ebec2711adac4e95a61314604447bc7

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents


module Antrapol
  module ToolRack
    module ConditionUtils

      def is_empty?(obj)
        if not defined?(obj)
          true
        elsif obj.nil?
          true
        elsif obj.respond_to?(:empty?)
          if obj.respond_to?(:strip)
            obj.strip.empty?
          else
            obj.empty?
          end
        elsif obj.respond_to?(:length)
          obj.length == 0
        elsif obj.respond_to?(:size)
          obj.size == 0
        else
          false
        end
      end # is_empty?

      def not_empty?(obj)
        !is_empty?(obj)
      end # not empty

      def is_boolean?(val)
        !!val == val
      end
      alias_method :is_bool?, :is_boolean?

      def is_string_boolean?(str)
        if not_empty?(str)
          s = str.to_s.strip.downcase
          case s
          when "true", "false"
            true
          else
            false
          end
        else
          false
        end
      end
      alias_method :is_str_bool?, :is_string_boolean?

      def self.included(klass)
        klass.class_eval <<-END
          extend Antrapol::ToolRack::ConditionUtils
        END
      end

    end # ConditionUtils
  end # MyToolRack
end # Antrapol

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
toolrack-0.16.0 lib/toolrack/condition_utils.rb
toolrack-0.15.0 lib/toolrack/condition_utils.rb
toolrack-0.14.0 lib/toolrack/condition_utils.rb
toolrack-0.13.0 lib/toolrack/condition_utils.rb