Module: Jsof::WrapHelper

Defined in:
lib/jsof/wrap_helper.rb

Overview

Helper methods.

Class Method Summary collapse

Class Method Details

.assignable?(type, val) ⇒ Boolean

Check value can be wrapped by specific type.

Parameters:

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jsof/wrap_helper.rb', line 52

def self.assignable?(type, val)
  return true unless val

  if type.is_a? Jsof::WrapArrayType
    return type.assignable?(val)
  end
  if type == Jsof::BooleanType
    return val == true || val == false
  end
  if type < Jsof::WrapObject
    return type.assignable?(val)
  end
  if type.is_a? Class
    return val.is_a? type
  end
  true
end

.boxing(val, wrapper_type) ⇒ Object

Wrap value if wrappable.

Parameters:

  • val (Object)

    Wrap target object

  • Wrapper (Class, Jsof::WrapArrayType)

    type. if nil, use WrapObject/WrapArray.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jsof/wrap_helper.rb', line 27

def self.boxing(val, wrapper_type)
  if require_wrap_value?(val)
    return wrapper_type.new(val) if wrapper_type
    return Jsof::WrapObject.new(val)
  end
  if require_array_wrap_value?(val)
    return wrapper_type.new(val) if wrapper_type
    return Jsof::WrapArray.new(val)
  end
  return val
end

.require_array_wrap_value?(val) ⇒ Boolean

Returns Wrappable by Jsof::WrapArray or not.

Returns:

  • (Boolean)

    Wrappable by Jsof::WrapArray or not



19
20
21
22
# File 'lib/jsof/wrap_helper.rb', line 19

def self.require_array_wrap_value?(val)
  return true if val.is_a?(Array)
  return false
end

.require_wrap_value?(val) ⇒ Boolean

Returns Wrappable by Jsof::WrapObject or not.

Returns:

  • (Boolean)

    Wrappable by Jsof::WrapObject or not



13
14
15
16
# File 'lib/jsof/wrap_helper.rb', line 13

def self.require_wrap_value?(val)
  return true if val.is_a?(Hash)
  return false
end

.unboxing(val) ⇒ Object

Take internal object if wrapped.

Parameters:

  • val (Object)

    Unwrap target object



41
42
43
44
45
46
# File 'lib/jsof/wrap_helper.rb', line 41

def self.unboxing(val)
  if wrapped_value?(val)
    return val.internal_object
  end
  return val
end

.wrapped_value?(val) ⇒ Boolean

Returns Already wrapped or not.

Returns:

  • (Boolean)

    Already wrapped or not



6
7
8
9
10
# File 'lib/jsof/wrap_helper.rb', line 6

def self.wrapped_value?(val)
  return true if val.is_a?(Jsof::WrapObject)
  return true if val.is_a?(Jsof::WrapArray)
  return false
end