Class: Object

Inherits:
BasicObject
Defined in:
lib/volt/extra_core/true_false.rb,
lib/volt/extra_core/blank.rb,
lib/volt/extra_core/object.rb,
lib/volt/extra_core/stringify_keys.rb

Overview

The true?/false? predicates are convience methods since if ..reactive_value.. does not correctly evaluate. The reason for this is that ruby currently does not allow anything besides nil and false to be falsy.

Instance Method Summary collapse

Instance Method Details

#and(other) ⇒ Object

Provides the same functionality as &&, treats a nil model as falsy



19
20
21
22
23
24
25
# File 'lib/volt/extra_core/object.rb', line 19

def and(other)
  if self && !self.nil?
    return other
  else
    return self
  end
end

#blank?Boolean

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

This simplifies:

if address.nil? || address.empty?

…to:

if address.blank?

Returns:



12
13
14
# File 'lib/volt/extra_core/blank.rb', line 12

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

#deep_cloneObject

TODO: Need a real implementation of this



32
33
34
# File 'lib/volt/extra_core/object.rb', line 32

def deep_clone
  Marshal.load(Marshal.dump(self))
end

#false?Boolean

Returns:



10
11
12
# File 'lib/volt/extra_core/true_false.rb', line 10

def false?
  false
end

#html_inspectObject



27
28
29
# File 'lib/volt/extra_core/object.rb', line 27

def html_inspect
  inspect.gsub('<', '&lt;').gsub('>', '&gt;')
end

#instance_valuesObject

Setup a default pretty_inspect alias_method :pretty_inspect, :inspect



5
6
7
# File 'lib/volt/extra_core/object.rb', line 5

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

#or(other) ⇒ Object

Provides the same functionality as ||, but treats a nil model as falsy



10
11
12
13
14
15
16
# File 'lib/volt/extra_core/object.rb', line 10

def or(other)
  if self && !self.nil?
    return self
  else
    return other
  end
end

#present?Boolean

An object is present if it’s not blank?.

Returns:



17
18
19
# File 'lib/volt/extra_core/blank.rb', line 17

def present?
  !blank?
end

#stringify_keysObject



2
3
4
5
6
# File 'lib/volt/extra_core/stringify_keys.rb', line 2

def stringify_keys
  each_with_object({}) do |(key, value), hash|
    hash[key.to_s] = value
  end
end

#symbolize_keysObject



8
9
10
11
12
# File 'lib/volt/extra_core/stringify_keys.rb', line 8

def symbolize_keys
  each_with_object({}) do |(key, value), hash|
    hash[key.to_sym] = value
  end
end

#true?Boolean

Returns:



6
7
8
# File 'lib/volt/extra_core/true_false.rb', line 6

def true?
  true
end