Module: Opal
- Defined in:
- opal/opal/corelib/helpers.rb
Class Method Summary collapse
- .coerce_to(object, type, method) ⇒ Object
- .coerce_to!(object, type, method) ⇒ Object
- .coerce_to?(object, type, method) ⇒ Boolean
- .compare(a, b) ⇒ Object
- .destructure(args) ⇒ Object
- .inspect(obj) ⇒ Object
- .respond_to?(obj, method) ⇒ Boolean
- .try_convert(object, type, method) ⇒ Object
- .type_error(object, type, method = nil, coerced = nil) ⇒ Object
Class Method Details
.coerce_to(object, type, method) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'opal/opal/corelib/helpers.rb', line 10 def self.coerce_to(object, type, method) return object if type === object unless object.respond_to? method raise type_error(object, type) end object.__send__ method end |
.coerce_to!(object, type, method) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'opal/opal/corelib/helpers.rb', line 20 def self.coerce_to!(object, type, method) coerced = coerce_to(object, type, method) unless type === coerced raise type_error(object, type, method, coerced) end coerced end |
.coerce_to?(object, type, method) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'opal/opal/corelib/helpers.rb', line 30 def self.coerce_to?(object, type, method) return unless object.respond_to? method coerced = coerce_to(object, type, method) return if coerced.nil? unless type === coerced raise type_error(object, type, method, coerced) end coerced end |
.compare(a, b) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'opal/opal/corelib/helpers.rb', line 52 def self.compare(a, b) compare = a <=> b if `compare === nil` raise ArgumentError, "comparison of #{a.class} with #{b.class} failed" end compare end |
.destructure(args) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'opal/opal/corelib/helpers.rb', line 62 def self.destructure(args) %x{ if (args.length == 1) { return args[0]; } else if (args.$$is_array) { return args; } else { return $slice.call(args); } } end |
.inspect(obj) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'opal/opal/corelib/helpers.rb', line 86 def self.inspect(obj) %x{ if (obj === undefined) { return "undefined"; } else if (obj === null) { return "null"; } else if (!obj.$$class) { return obj.toString(); } else { return #{obj.inspect}; } } end |
.respond_to?(obj, method) ⇒ Boolean
76 77 78 79 80 81 82 83 84 |
# File 'opal/opal/corelib/helpers.rb', line 76 def self.respond_to?(obj, method) %x{ if (obj == null || !obj.$$class) { return false; } } obj.respond_to? method end |
.try_convert(object, type, method) ⇒ Object
44 45 46 47 48 49 50 |
# File 'opal/opal/corelib/helpers.rb', line 44 def self.try_convert(object, type, method) return object if type === object if object.respond_to? method object.__send__ method end end |
.type_error(object, type, method = nil, coerced = nil) ⇒ Object
2 3 4 5 6 7 8 |
# File 'opal/opal/corelib/helpers.rb', line 2 def self.type_error(object, type, method = nil, coerced = nil) if method && coerced TypeError.new "can't convert #{object.class} into #{type} (#{object.class}##{method} gives #{coerced.class}" else TypeError.new "no implicit conversion of #{object.class} into #{type}" end end |