Module: Opal
- Defined in:
- opal/opal/corelib/helpers.rb
Class Method Summary collapse
- .bridge(klass, constructor) ⇒ Object
- .coerce_to(object, type, method) ⇒ Object
- .coerce_to!(object, type, method) ⇒ Object
- .coerce_to?(object, type, method) ⇒ Boolean
- .compare(a, b) ⇒ Object
- .const_name!(const_name) ⇒ Object
- .destructure(args) ⇒ Object
- .inspect(obj) ⇒ Object
- .instance_variable_name!(name) ⇒ Object
-
.pristine(owner_class, *method_names) ⇒ nil
Mark some methods as pristine in order to apply optimizations when they are still in their original form.
- .respond_to?(obj, method) ⇒ Boolean
- .try_convert(object, type, method) ⇒ Object
- .type_error(object, type, method = nil, coerced = nil) ⇒ Object
Class Method Details
.bridge(klass, constructor) ⇒ Object
2 3 4 |
# File 'opal/opal/corelib/helpers.rb', line 2 def self.bridge(klass, constructor) `Opal.bridge(klass, constructor)` end |
.coerce_to(object, type, method) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'opal/opal/corelib/helpers.rb', line 14 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
24 25 26 27 28 29 30 31 32 |
# File 'opal/opal/corelib/helpers.rb', line 24 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
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'opal/opal/corelib/helpers.rb', line 34 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
56 57 58 59 60 61 62 63 64 |
# File 'opal/opal/corelib/helpers.rb', line 56 def self.compare(a, b) compare = a <=> b if `compare === nil` raise ArgumentError, "comparison of #{a.class} with #{b.class} failed" end compare end |
.const_name!(const_name) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'opal/opal/corelib/helpers.rb', line 120 def self.const_name!(const_name) const_name = Opal.coerce_to!(const_name, String, :to_str) if const_name[0] != const_name[0].upcase raise NameError, "wrong constant name #{const_name}" end const_name end |
.destructure(args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'opal/opal/corelib/helpers.rb', line 66 def self.destructure(args) %x{ if (args.length == 1) { return args[0]; } else if (args.$$is_array) { return args; } else { var args_ary = new Array(args.length); for(var i = 0, l = args_ary.length; i < l; i++) { args_ary[i] = args[i]; } return args_ary; } } end |
.inspect(obj) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'opal/opal/corelib/helpers.rb', line 93 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 |
.instance_variable_name!(name) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'opal/opal/corelib/helpers.rb', line 110 def self.instance_variable_name!(name) name = Opal.coerce_to!(name, String, :to_str) unless `/^@[a-zA-Z_][a-zA-Z0-9_]*?$/.test(name)` raise NameError.new("'#{name}' is not allowed as an instance variable name", name) end name end |
.pristine(owner_class, *method_names) ⇒ nil
Mark some methods as pristine in order to apply optimizations when they
are still in their original form. This could probably be moved to
the Opal.def()
JS API, but for now it will stay manual.
156 157 158 159 160 161 162 163 164 165 |
# File 'opal/opal/corelib/helpers.rb', line 156 def self.pristine owner_class, *method_names %x{ var method_name; for (var i = method_names.length - 1; i >= 0; i--) { method_name = method_names[i]; owner_class.$$proto['$'+method_name].$$pristine = true } } nil end |
.respond_to?(obj, method) ⇒ Boolean
83 84 85 86 87 88 89 90 91 |
# File 'opal/opal/corelib/helpers.rb', line 83 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
48 49 50 51 52 53 54 |
# File 'opal/opal/corelib/helpers.rb', line 48 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
6 7 8 9 10 11 12 |
# File 'opal/opal/corelib/helpers.rb', line 6 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 |