Sha256: 2b0e64c8d4968f53ee49a64ff363291c185ca5766072b21e23be62b6b866000a

Contents?: true

Size: 1.86 KB

Versions: 20

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module ActiveObject
  module Object
    FALSE_VALUES ||= [false, 0, '0', 'false', 'FALSE', 'f', 'F'].freeze
    TRUE_VALUES ||= [true, 1, '1', 'true', 'TRUE', 't', 'T'].freeze

    def array?
      is_a?(Array)
    end

    def blank?
      object = self
      object = object.strip if respond_to?(:strip)
      respond_to?(:empty?) ? object.empty? : !object
    end

    def boolean?
      TRUE_VALUES.include?(self) || FALSE_VALUES.include?(self)
    end

    # rubocop:disable Style/YodaCondition
    def false?
      false == self
    end
    # rubocop:enable Style/YodaCondition

    def falsey?
      nil? || FALSE_VALUES.include?(self)
    end

    def float?
      is_a?(Float)
    end

    def hash?
      is_a?(Hash)
    end

    def integer?
      is_a?(Integer)
    end

    def numeral?
      !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
    end

    def numeric?
      is_a?(Numeric)
    end

    def palindrome?
      to_s == to_s.reverse
    end

    def present?
      !blank?
    end

    def range?
      is_a?(Range)
    end

    def salvage(placeholder = '---')
      blank? ? placeholder : self
    end

    def send_chain(*keys)
      Array(keys).inject(self) { |obj, key| obj.send(*key) }
    end

    def string?
      is_a?(String)
    end

    def time?
      is_a?(Time)
    end

    # rubocop:disable Style/YodaCondition
    def true?
      true == self
    end
    # rubocop:enable Style/YodaCondition

    def truthy?
      TRUE_VALUES.include?(self)
    end

    def try(*obj, &block)
      try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
    end

    def try!(*obj, &block)
      if obj.empty? && block_given?
        block.arity.zero? ? instance_eval(&block) : yield(self)
      else
        public_send(*obj, &block)
      end
    end

  end
end

Object.include(ActiveObject::Object) if ActiveObject.configuration.autoload_object

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
active_object-5.7.0 lib/active_object/object.rb
active_object-5.6.0 lib/active_object/object.rb
active_object-5.5.3 lib/active_object/object.rb
active_object-5.5.2 lib/active_object/object.rb
active_object-5.5.1 lib/active_object/object.rb
active_object-5.5.0 lib/active_object/object.rb
active_object-5.4.0 lib/active_object/object.rb
active_object-5.3.1 lib/active_object/object.rb
active_object-5.3.0 lib/active_object/object.rb
active_object-5.2.5 lib/active_object/object.rb
active_object-5.2.4 lib/active_object/object.rb
active_object-5.2.3 lib/active_object/object.rb
active_object-5.2.2 lib/active_object/object.rb
active_object-5.2.1 lib/active_object/object.rb
active_object-5.2.0 lib/active_object/object.rb
active_object-5.1.2 lib/active_object/object.rb
active_object-5.1.1 lib/active_object/object.rb
active_object-5.1.0 lib/active_object/object.rb
active_object-5.0.2 lib/active_object/object.rb
active_object-5.0.1 lib/active_object/object.rb