Module: Steroids::Extensions::ObjectExtension

Defined in:
lib/steroids/extensions/object_extension.rb

Instance Method Summary collapse

Instance Method Details

#deep_serialize(include_object = true) ⇒ Object

Raises:

  • (TypeError)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/steroids/extensions/object_extension.rb', line 100

def deep_serialize(include_object = true)
  raise TypeError, "Cannot serialize object of type #{self.class}" unless serializable?(include_object)

  case self
  when Hash
    self.to_h.transform_values { |value| value.deep_serialize(include_object) }
  when Array
    self.map { |value| value.deep_serialize(include_object) }
  when String, Symbol, Numeric, TrueClass, FalseClass, NilClass
    self
  else
    if include_object && self.respond_to?(:to_h)
      self.to_h.deep_serialize(include_object)
    elsif include_object && self.respond_to?(:as_json)
      self.as_json.deep_serialize(include_object)
    end
  end
end

#ifnil(default) ⇒ Object


If nil default




73
74
75
# File 'lib/steroids/extensions/object_extension.rb', line 73

def ifnil(default)
  itself == nil ? default : itself
end

#instance_apply(*given_arguments, **given_options, &block) ⇒ Object


Apply and send




8
9
10
11
12
# File 'lib/steroids/extensions/object_extension.rb', line 8

def instance_apply(*given_arguments, **given_options, &block)
  applied_arguments = dynamic_arguments_for(block, given_arguments)
  applied_options = dynamic_options_for(block, given_options)
  self.instance_exec(*applied_arguments, **applied_options, &block)
end

#marshallable?Boolean


Marshall dump


Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/steroids/extensions/object_extension.rb', line 34

def marshallable?
  !!Marshal.dump(self)
rescue TypeError
  false
end

#send_apply(method_name, *given_arguments, **given_options, &block) ⇒ Object

TODO: Rename to try_apply and implement/alias try_send



15
16
17
18
19
20
21
# File 'lib/steroids/extensions/object_extension.rb', line 15

def send_apply(method_name, *given_arguments, **given_options, &block)
  return unless respond_to?(method_name, true)
  method = method(method_name)
  applied_arguments = method.dynamic_arguments_for(given_arguments, given_options)
  applied_options = method.dynamic_options_for(given_options)
  self.send(method_name, *applied_arguments, **applied_options, &block)
end

#send_apply!(method_name, *given_arguments, **given_options, &block) ⇒ Object

TODO: Rename to send_apply



24
25
26
27
28
# File 'lib/steroids/extensions/object_extension.rb', line 24

def send_apply!(method_name, *given_arguments, **given_options, &block)
  return NoMethodError.new("Send apply", method_name) unless self.respond_to?(method_name, true)

  send_apply(method_name, *given_arguments, **given_options, &block)
end

#serializable?(include_object = true) ⇒ Boolean


Serialization


Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/steroids/extensions/object_extension.rb', line 81

def serializable?(include_object = true)
  if self.is_a?(Hash)
    self.all? do |key, value|
      key.serializable?(include_object) && value.serializable?(include_object)
    end
  elsif self.is_a?(Array)
    self.all? do |value|
      value.serializable?(include_object)
    end
  elsif self.is_a?(String) || self.is_a?(Symbol) || self.is_a?(Numeric) ||
    self.is_a?(TrueClass) || self.is_a?(FalseClass) || self.is_a?(NilClass)
    true
  elsif include_object == true
    self.respond_to?(:as_json) && self.as_json.serializable?(include_object)
  else
    false
  end
end

#try_method(method_name) ⇒ Object


Try method




44
45
46
47
48
# File 'lib/steroids/extensions/object_extension.rb', line 44

def try_method(method_name)
  if self.respond_to?(method_name, true)
    self.method(method_name)
  end
end

#typed(expected_type) ⇒ Object


Type




54
55
56
# File 'lib/steroids/extensions/object_extension.rb', line 54

def typed(expected_type)
  return itself if instance_of?(expected_type) || itself == nil
end

#typed!(expected_type) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/steroids/extensions/object_extension.rb', line 58

def typed!(expected_type)
  typed_itself = typed(expected_type)
  return typed_itself if typed_itself == itself

  message = "Expected #{self.inspect} to be an instance of #{expected_type.inspect}"
  TypeError.new(message).tap do |exception|
    exception.set_backtrace(caller)
    raise exception
  end
end