Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/volt/extra_core/blank.rb,
lib/volt/extra_core/object.rb,
lib/volt/extra_core/stringify_keys.rb
Instance Method Summary collapse
-
#blank? ⇒ Boolean
An object is blank if it’s false, empty, or a whitespace string.
-
#deep_clone ⇒ Object
TODO: Need a real implementation of this.
- #html_inspect ⇒ Object
-
#instance_values ⇒ Object
Setup a default pretty_inspect alias_method :pretty_inspect, :inspect.
-
#present? ⇒ Boolean
An object is present if it’s not
blank?
. - #stringify_keys ⇒ Object
- #symbolize_keys ⇒ Object
-
#then(&block) ⇒ Object
Convert a non-promise value into a resolved promise.
-
#try(*a, &b) ⇒ Object
def fail(&block) promisify_and_run_method(:fail, &block) end.
Instance Method Details
#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?
12 13 14 |
# File 'lib/volt/extra_core/blank.rb', line 12 def blank? respond_to?(:empty?) ? empty? : !self end |
#deep_clone ⇒ Object
TODO: Need a real implementation of this
16 17 18 19 20 21 22 |
# File 'lib/volt/extra_core/object.rb', line 16 def deep_clone if RUBY_PLATFORM == 'opal' Volt::EJSON.parse(Volt::EJSON.stringify(self)) else Marshal.load(Marshal.dump(self)) end end |
#html_inspect ⇒ Object
11 12 13 |
# File 'lib/volt/extra_core/object.rb', line 11 def html_inspect inspect.gsub('<', '<').gsub('>', '>') end |
#instance_values ⇒ Object
Setup a default pretty_inspect alias_method :pretty_inspect, :inspect
7 8 9 |
# File 'lib/volt/extra_core/object.rb', line 7 def instance_values Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] }] end |
#present? ⇒ Boolean
An object is present if it’s not blank?
.
17 18 19 |
# File 'lib/volt/extra_core/blank.rb', line 17 def present? !blank? end |
#stringify_keys ⇒ Object
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_keys ⇒ Object
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 |
#then(&block) ⇒ Object
Convert a non-promise value into a resolved promise. Resolve the block if it takes one.
26 27 28 |
# File 'lib/volt/extra_core/object.rb', line 26 def then(&block) promisify_and_run_method(:then, &block) end |
#try(*a, &b) ⇒ Object
def fail(&block)
promisify_and_run_method(:fail, &block)
end
34 35 36 37 38 39 40 |
# File 'lib/volt/extra_core/object.rb', line 34 def try(*a, &b) if a.empty? && block_given? yield self else public_send(*a, &b) if respond_to?(a.first) end end |