Skip to content

Commit

Permalink
Added basic Terminal-Table output for Models,
Browse files Browse the repository at this point in the history
Supported: Basic Contracts
  • Loading branch information
Hartmut Bischoff committed Jun 9, 2021
1 parent 016b580 commit de85c41
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_dependency 'activesupport', '>= 6.0'
spec.add_dependency 'activemodel'
spec.add_dependency 'terminal-table'
end
9 changes: 9 additions & 0 deletions lib/extensions/class-extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ def count_duplicates
self.each_with_object(Hash.new(0)) { |element, counter| counter[element] += 1 }.sort_by{|k,v| -v}.to_h
end
end

module TablePresenter
def as_table
the_table_header = first.table_header
the_table_rows = map &:table_row
Terminal::Table.new headings: the_table_header, rows: the_table_rows , style: { border: :unicode }
end
end
end
end

Array.include CoreExtensions::Array::DuplicatesCounter
Array.include CoreExtensions::Array::TablePresenter



Expand Down
14 changes: 13 additions & 1 deletion lib/ib/base_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@ def to_human
end.compact.sort.join(' ') + ">"
end

def table_header
[ self.class.to_s.demodulize ] + content_attributes.keys
end
def table_row
[ self.class.to_s.demodulize ] + content_attributes.values
end


def as_table
Terminal::Table.new headings: table_header, rows: [table_row ], style: { border: :unicode }
end

# Comparison support
def content_attributes
#NoMethodError if a Hash is assigned to an attribute
Hash[attributes.reject do |(attr, _)|
attr.to_s =~ /(_count)\z/ ||
[:created_at, :type, # :updated_at,
[:created_at, :type, :updated_at,
:id, :order_id, :contract_id].include?(attr.to_sym)
end]
end
Expand Down
17 changes: 17 additions & 0 deletions lib/models/ib/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,23 @@ def to_human
].compact.join(" ") + ">"
end

def table_header
[ '', 'symbol', 'con_id', 'exchange', 'expiry','multiplier', 'trading-class' , 'right', 'strike', 'currency' ]
end

def table_row
[ self.class.to_s.demodulize, symbol,
con_id.zero? ? '' : con_id ,
{ value: exchange, alignment: :center},
expiry,
{ value: multiplier.zero?? "" : multiplier, alignment: :center},
{ value: trading_class, alignment: :center},
{value: right == :none ? "": right, alignment: :center },
{ value: strike.zero? ? "": strike, alignment: :right},
{ value: currency, alignment: :center} ]

end

def to_short
if expiry.blank? && last_trading_day.blank?
"#{symbol}# {exchange}# {currency}"
Expand Down
1 change: 1 addition & 0 deletions lib/requires.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'active_support/core_ext/module/attribute_accessors.rb'
require 'extensions/class-extensions'

require 'terminal-table'

require 'ib/version'
require 'ib/errors'
Expand Down

0 comments on commit de85c41

Please sign in to comment.