Module: D3::Native::Helpers

Defined in:
lib/opal/d3/native.rb

Instance Method Summary collapse

Instance Method Details

#alias_native_chainable(ruby_name, js_name = ruby_name) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/opal/d3/native.rb', line 66

def alias_native_chainable(ruby_name, js_name=ruby_name)
  eval <<-EOF
    def #{ruby_name}(*args)
      @native.JS.#{js_name}(*args)
      self
    end
  EOF
end

#alias_native_new(ruby_name, js_name = ruby_name) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/opal/d3/native.rb', line 75

def alias_native_new(ruby_name, js_name=ruby_name)
  eval <<-EOF
    def #{ruby_name}(*args)
      self.class.new @native.JS.#{js_name}(*args)
    end
  EOF
end

#attribute_d3(ruby_name, js_name = ruby_name) ⇒ Object

This provides ruby style and jquery style interfaces: obj.foo obj.foo = 1; obj.bar = 2 obj.foo(1).bar(2)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/opal/d3/native.rb', line 20

def attribute_d3(ruby_name, js_name=ruby_name)
  eval <<-EOF
    def #{ruby_name}(new_value=`undefined`)
      if `new_value !== undefined`
        new_value = `null` if new_value == nil
        @native.JS.#{js_name}(new_value)
        self
      else
        value = @native.JS.#{js_name}
        `value === null ? nil : value`
      end
    end
    def #{ruby_name}=(new_value)
      new_value = `null` if new_value == nil
      @native.JS.#{js_name}(new_value)
    end
  EOF
end

#attribute_d3_block(ruby_name, js_name = ruby_name) ⇒ Object

This provides ruby style and jquery style interfaces, and also block interface: obj.foo obj.foo = 1; obj.bar = 2 obj.foo(1).bar(2).buzz…



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/opal/d3/native.rb', line 44

def attribute_d3_block(ruby_name, js_name=ruby_name)
  eval <<-EOF
    def #{ruby_name}(new_value=`undefined`, &block)
      if block_given?
        @native.JS.#{js_name}(block)
        self
      elsif `new_value !== undefined`
        new_value = `null` if new_value == nil
        @native.JS.#{js_name}(new_value)
        self
      else
        value = @native.JS.#{js_name}
        `value === null ? nil : value`
      end
    end
    def #{ruby_name}=(new_value)
      new_value = `null` if new_value == nil
      @native.JS.#{js_name}(new_value)
    end
  EOF
end