Class: Object

Inherits:
BasicObject
Defined in:
lib/simple_ext/object/try.rb,
lib/simple_ext/object/blank.rb,
lib/simple_ext/object/to_query.rb,
lib/simple_ext/object/inclusion.rb,
lib/simple_ext/object/instance_variables.rb

Instance Method Summary collapse

Instance Method Details

#blank?true, false

An object is blank if it’s false, empty, or a whitespace string. For example, nil, ”, ‘ ’, [], {}, and false are all blank.

This simplifies

!address || address.empty?

to

address.blank?

Returns:

  • (true, false)


17
18
19
# File 'lib/simple_ext/object/blank.rb', line 17

def blank?
  respond_to?(:empty?) ? !!empty? : !self
end

#in?(another_object) ⇒ Boolean

Returns true if this object is included in the argument. Argument must be any object which responds to #include?. Usage:

characters = ["Konata", "Kagami", "Tsukasa"]
"Konata".in?(characters) # => true

This will throw an ArgumentError if the argument doesn’t respond to #include?.

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/simple_ext/object/inclusion.rb', line 12

def in?(another_object)
  another_object.include?(self)
rescue NoMethodError
  raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
end

#instance_valuesObject

Returns a hash with string keys that maps instance variable names without “@” to their corresponding values.

class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}


14
15
16
# File 'lib/simple_ext/object/instance_variables.rb', line 14

def instance_values
  Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] }]
end

#instance_variable_namesObject

Returns an array of instance variable names as strings including “@”.

class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_variable_names # => ["@y", "@x"]


27
28
29
# File 'lib/simple_ext/object/instance_variables.rb', line 27

def instance_variable_names
  instance_variables.map(&:to_s)
end

#presenceObject

Returns the receiver if it’s present otherwise returns nil. object.presence is equivalent to

object.present? ? object : nil

For example, something like

state   = params[:state]   if params[:state].present?
country = params[:country] if params[:country].present?
region  = state || country || 'US'

becomes

region = params[:state].presence || params[:country].presence || 'US'

Returns:



44
45
46
# File 'lib/simple_ext/object/blank.rb', line 44

def presence
  self if present?
end

#presence_in(another_object) ⇒ Object

Returns the receiver if it’s included in the argument otherwise returns nil. Argument must be any object which responds to #include?. Usage:

params[:bucket_type].presence_in %w( project calendar )

This will throw an ArgumentError if the argument doesn’t respond to #include?.

Returns:



26
27
28
# File 'lib/simple_ext/object/inclusion.rb', line 26

def presence_in(another_object)
  in?(another_object) ? self : nil
end

#present?true, false

An object is present if it’s not blank.

Returns:

  • (true, false)


24
25
26
# File 'lib/simple_ext/object/blank.rb', line 24

def present?
  !blank?
end

#to_paramObject

Alias of to_s.



7
8
9
# File 'lib/simple_ext/object/to_query.rb', line 7

def to_param
  to_s
end

#to_query(key) ⇒ Object

Converts an object into a string suitable for use as a URL query string, using the given key as the param name.



13
14
15
# File 'lib/simple_ext/object/to_query.rb', line 13

def to_query(key)
  "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
end

#try(method_name = nil, *args, &b) ⇒ Object

:method: try!

:call-seq:

try!(*a, &b)

Same as #try, but raises a NoMethodError exception if the receiver is not nil and does not implement the tried method.

"a".try!(:upcase) # => "A"
nil.try!(:upcase) # => nil
123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/simple_ext/object/try.rb', line 81

def try(method_name = nil, *args, &b)
  if method_name.nil? && block_given?
    if b.arity == 0
      instance_eval(&b)
    else
      yield self
    end
  elsif respond_to?(method_name)
    public_send(method_name, *args, &b)
  end
end

#try!(method_name = nil, *args, &b) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/simple_ext/object/try.rb', line 93

def try!(method_name = nil, *args, &b)
  if method_name.nil? && block_given?
    if b.arity == 0
      instance_eval(&b)
    else
      yield self
    end
  else
    public_send(method_name, *args, &b)
  end
end