Class: IbRubyProxy::Server::IbRubyClassSourceGenerator

Inherits:
Object
  • Object
show all
Includes:
Util::StringUtils
Defined in:
lib/ib_ruby_proxy/server/ib_ruby_class_source_generator.rb

Overview

Given a value object class from Interactive Broker API, this generator can build Ruby source code for:

  • A ruby class to manipulate the same class in Ruby

  • An extension to the original Java class for converting instances into Ruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::StringUtils

#to_camel_case, #to_underscore

Constructor Details

#initialize(ib_class, namespace: 'IbRubyProxy::Client::Ib') ⇒ IbRubyClassSourceGenerator

Returns a new instance of IbRubyClassSourceGenerator.

Parameters:

  • ib_class (Class)

    Class from IB broker to mimic in Ruby. It is meant to be used with value objects, as only properties are translated. That is the case for classes that represent either arguments or returned values (via callbacks) in the API.

  • namespace (String) (defaults to: 'IbRubyProxy::Client::Ib')

    The namespace to generate the classes in. Default IbRubyProxy::Client::Ib



18
19
20
21
# File 'lib/ib_ruby_proxy/server/ib_ruby_class_source_generator.rb', line 18

def initialize(ib_class, namespace: 'IbRubyProxy::Client::Ib')
  @ib_class = Reflection::IbClass.new(ib_class)
  @namespace_list = namespace.split('::')
end

Instance Attribute Details

#ib_classObject (readonly)

Returns the value of attribute ib_class.



11
12
13
# File 'lib/ib_ruby_proxy/server/ib_ruby_class_source_generator.rb', line 11

def ib_class
  @ib_class
end

#namespace_listObject (readonly)

Returns the value of attribute namespace_list.



11
12
13
# File 'lib/ib_ruby_proxy/server/ib_ruby_class_source_generator.rb', line 11

def namespace_list
  @namespace_list
end

Instance Method Details

#ib_class_extension_sourceString

Source for a ruby class that extends the original IB Java class with a to_ruby method, to convert Java Ib objects into their Ruby counterparts generated in #ruby_class_source

Returns:

  • (String)


43
44
45
46
47
48
49
# File 'lib/ib_ruby_proxy/server/ib_ruby_class_source_generator.rb', line 43

def ib_class_extension_source
  <<-RUBY
  #{header}

  #{generate_ib_class_extension}
  RUBY
end

#ruby_class_sourceString

Source for a ruby class translates into Ruby the Java properties of the target ib class.

It generates a Struct with the list of properties, that admits a keyword-based constructor, and that includes a to_ib method for converting the ruby back into its original Java counterpart.

Returns:

  • (String)


31
32
33
34
35
36
37
# File 'lib/ib_ruby_proxy/server/ib_ruby_class_source_generator.rb', line 31

def ruby_class_source
  <<-RUBY
  #{header}

  #{generate_ruby_class}
  RUBY
end