Sha256: 539156540233adde37cda7d335a01ea9b308d8dd1fbcd42da2d1c95779a76e3f

Contents?: true

Size: 758 Bytes

Versions: 3

Compression:

Stored size: 758 Bytes

Contents

class Object
  def as(&block)
    yield self
  end
  
  # Logic utilities
  def or_else(&block)
    self || yield(self)
  end
  
  def and_also(&block)
    self && yield(self)
  end
  
  # Convenience methods for a more idiomatic code than [ "users", "purchases" ].include?(controller_name) so that
  # the same can now be written as controller_name.is_included_in?('users', 'purchases')
  def is_included_in?(*others)
    others.flatten_splat.include?(self)
  end
  
  def is_excluded_from?(*others)
    others.flatten_splat.exclude?(self)
  end

  # Similar to is_a? or kind_of? but with an array of possible classes. Returns the matching class or a nil.
  def is_one_kind_of?(*klasses)
    klasses.flatten_splat.detect {|klass| klass === self }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
core_utilities-0.1.3 lib/core_utilities/core_ext/object.rb
core_utilities-0.1.1 lib/core_utilities/core_ext/object.rb
core_utilities-0.1.0 lib/core_utilities/core_ext/object.rb