Sha256: f5e5d84ae669c47abaa34cb8c837e9f3e2f7dabe13ca0cab1da3788c30eac890

Contents?: true

Size: 1.1 KB

Versions: 58

Compression:

Stored size: 1.1 KB

Contents

module Vagrant
  module Util
    module Presence
      extend self

      # Determines if the given object is "present". A String is considered
      # present if the stripped contents are not empty. An Array/Hash is
      # considered present if they have a length of more than 1. "true" is
      # always present and `false` and `nil` are always not present. Any other
      # object is considered to be present.
      #
      # @return [true, false]
      def present?(obj)
        case obj
        when String
          !obj.strip.empty?
        when Symbol
          !obj.to_s.strip.empty?
        when Array
          !obj.compact.empty?
        when Hash
          !obj.empty?
        when TrueClass, FalseClass
          obj
        when NilClass
          false
        when Object
          true
        end
      end

      # Returns the presence of the object. If the object is {present?}, it is
      # returned. Otherwise `false` is returned.
      #
      # @return [Object, false]
      def presence(obj)
        if present?(obj)
          obj
        else
          false
        end
      end
    end
  end
end

Version data entries

58 entries across 54 versions & 6 rubygems

Version Path
vagrant-unbundled-2.2.0.0 lib/vagrant/util/presence.rb
vagrant-unbundled-2.1.4.0 lib/vagrant/util/presence.rb
vagrant-unbundled-2.1.2.0 lib/vagrant/util/presence.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/presence.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/presence.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/presence.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/presence.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/presence.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/presence.rb
vagrant-unbundled-2.1.1.0 lib/vagrant/util/presence.rb
vagrant-unbundled-2.0.4.0 lib/vagrant/util/presence.rb
vagrant-unbundled-2.0.3.0 lib/vagrant/util/presence.rb
vagrant-aws-detiber-0.7.2.pre.4 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/lib/vagrant/util/presence.rb
vagrant-aws-detiber-0.7.2.pre.3 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/lib/vagrant/util/presence.rb
vagrant-aws-detiber-0.7.2.pre.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/lib/vagrant/util/presence.rb
vagrant-unbundled-2.0.2.0 lib/vagrant/util/presence.rb
vagrant-unbundled-2.0.1.0 lib/vagrant/util/presence.rb
vagrant-aws-mkubenka-0.7.2.pre.22 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-f3fdbf414272/lib/vagrant/util/presence.rb
vagrant-aws-mkubenka-0.7.2.pre.16 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-1ee58c40e3f5/lib/vagrant/util/presence.rb
vagrant-unbundled-2.0.0.1 lib/vagrant/util/presence.rb