Skip to content
Browse files

Initial plumbing for db-backed Models

  • Loading branch information...
1 parent 5f06701 commit 48a2f702bd127438977710c994d24051f15b893a @arvicco committed
View
4 lib/ib-ruby.rb
@@ -1,4 +1,8 @@
module IB
+ # IB Models can be either database-backed, or not
+ # By default there is no DB backend, unless specifically requested
+ # require 'ib-ruby/db' # to make all IB models database-backed
+ DB ||= false
require 'ib-ruby/version'
require 'ib-ruby/extensions'
View
21 lib/ib-ruby/db.rb
@@ -0,0 +1,21 @@
+# By requiring this file, we make all IB:Models database-backed ActiveRecord subclasses
+
+module IB
+ module DB
+
+ # Establish DB connection and do other plumbing here
+ def self.connect config
+ log.warn "Starting Database connection"
+ ActiveRecord::Base.establish_connection(config)
+ ActiveRecord::Base.logger = log
+ #ActiveRecord.colorize_logging = false
+
+ # Get rid of nasty conversion issues
+ ActiveRecord::Base.default_timezone = :utc
+ Time.zone = 'UTC'
+ end
+ end # module DB
+end
+
+require 'ib-ruby'
+
View
10 lib/ib-ruby/models.rb
@@ -1,5 +1,15 @@
module IB
module Models
+
+ # IB Models can be either database-backed, or not
+ # require 'ib-ruby/db' # to make all IB models database-backed
+ if DB
+ require 'ib-ruby/models/db_model'
+ Model = DBModel # All IB Models will be subclassed from ActiveRecord::Base
+ else
+ require 'ib-ruby/models/model'
+ end
+
require 'ib-ruby/models/contracts'
# Flatten namespace (IB::Models::Option instead of IB::Models::Contracts::Option)
include Contracts
View
2 lib/ib-ruby/models/bar.rb
@@ -1,5 +1,3 @@
-require 'ib-ruby/models/model'
-
module IB
module Models
# This is a single data point delivered by HistoricData messages.
View
2 lib/ib-ruby/models/contracts/contract.rb
@@ -1,5 +1,3 @@
-require 'ib-ruby/models/model'
-
module IB
module Models
module Contracts
View
29 lib/ib-ruby/models/db_model.rb
@@ -0,0 +1,29 @@
+require 'ib-ruby/models/model_properties'
+
+module IB
+ module Models
+
+ # Base IB Model class derived from ActiveRecord
+ class DBModel < ActiveRecord::Base
+ extend ModelProperties
+
+ attr_reader :created_at
+
+ DEFAULT_PROPS = {}
+
+ # If a opts hash is given, keys are taken as attribute names, values as data.
+ # The model instance fields are then set automatically from the opts Hash.
+ def initialize(opts={})
+
+ check_that_match_all props, columns
+
+ error "Argument must be a Hash", :args unless opts.is_a?(Hash)
+ @created_at = Time.now
+
+ props = self.class::DEFAULT_PROPS.merge(opts)
+ props.keys.each { |key| self.send("#{key}=", props[key]) }
+ end
+
+ end # Model
+ end # module Models
+end # module IB
View
2 lib/ib-ruby/models/execution.rb
@@ -1,5 +1,3 @@
-require 'ib-ruby/models/model'
-
module IB
module Models
# This is IB Order execution report.
View
20 lib/ib-ruby/models/order.rb
@@ -1,7 +1,3 @@
-require 'ib-ruby/models/model'
-
-# TODO: Implement equals() according to the criteria in IB's Java client.
-
module IB
module Models
class Order < Model
@@ -285,29 +281,29 @@ class Order < Model
# OrderState object. Here, they are lumped into Order proper: see OrderState.java
# TODO: Extract OrderState object, for better record keeping
prop :status, # String: Displays the order status.Possible values include:
- # • PendingSubmit - indicates that you have transmitted the order, but
+ # PendingSubmit - indicates that you have transmitted the order, but
# have not yet received confirmation that it has been accepted by the
# order destination. NOTE: This order status is NOT sent back by TWS
# and should be explicitly set by YOU when an order is submitted.
- # • PendingCancel - indicates that you have sent a request to cancel
+ # PendingCancel - indicates that you have sent a request to cancel
# the order but have not yet received cancel confirmation from the
# order destination. At this point, your order cancel is not confirmed.
# You may still receive an execution while your cancellation request
# is pending. NOTE: This order status is not sent back by TWS and
# should be explicitly set by YOU when an order is canceled.
- # • PreSubmitted - indicates that a simulated order type has been
+ # PreSubmitted - indicates that a simulated order type has been
# accepted by the IB system and that this order has yet to be elected.
# The order is held in the IB system until the election criteria are
# met. At that time the order is transmitted to the order destination
# as specified.
- # • Submitted - indicates that your order has been accepted at the order
+ # Submitted - indicates that your order has been accepted at the order
# destination and is working.
- # • Cancelled - indicates that the balance of your order has been
+ # Cancelled - indicates that the balance of your order has been
# confirmed canceled by the IB system. This could occur unexpectedly
# when IB or the destination has rejected your order.
- # • ApiCancelled - canceled via API
- # • Filled - indicates that the order has been completely filled.
- # • Inactive - indicates that the order has been accepted by the system
+ # ApiCancelled - canceled via API
+ # Filled - indicates that the order has been completely filled.
+ # Inactive - indicates that the order has been accepted by the system
# (simulated orders) or an exchange (native orders) but that currently
# the order is inactive due to system, exchange or other issues.
:commission, # double: Shows the commission amount on the order.
View
7 spec/db.rb
@@ -0,0 +1,7 @@
+puts 'TODO: Run rspec with ActiveRecord version. Use:'
+puts '$ rspec -rdb spec'
+
+require 'ib-ruby/db'
+
+# Do other DB plumbing, like establishing connection to test DB
+# Set RSpec metadata to run AR-specific specs
View
2 spec/integration/orders/valid_ids_spec.rb
@@ -52,7 +52,7 @@
it_behaves_like 'Received single id'
- it 'receives also :OpenOrderEnd message', :pending => 'not in GW 924.2e' do
+ it 'receives also :OpenOrderEnd message', :pending => 'not in GW 924.3a' do
@ib.received[:OpenOrderEnd].should have_exactly(1).message
@ib.received[:OpenOrderEnd].first.should be_an IB::Messages::Incoming::OpenOrderEnd
end

0 comments on commit 48a2f70

Please sign in to comment.
Something went wrong with that request. Please try again.