Module: IB::BaseProperties
- Extended by:
- ActiveSupport::Concern
- Included in:
- Account, AccountValue, Bar, ComboLeg, Contract, ContractDetail, Execution, OptionDetail, Order, OrderCondition, OrderState, PortfolioValue, Underlying
- Defined in:
- lib/ib/base_properties.rb
Overview
Module adds prop Macro and
Instance Method Summary collapse
-
#==(other) ⇒ Object
Default Model comparison.
-
#as_table(&b) ⇒ Object
└───────────┴─────────────────────────────────────────────┴─────┴────────┴─────────┴──────────┴────────────┴──────────┘.
-
#content_attributes ⇒ Object
Comparison support.
-
#default_attributes ⇒ Object
Default attributes support.
-
#invariant_attributes ⇒ Object
Remove all Time-Stamps from the list of Attributes.
- #set_attribute_defaults ⇒ Object
- #table_header ⇒ Object
- #table_row ⇒ Object
-
#to_human ⇒ Object
Default presentation.
-
#update_missing(attrs) ⇒ Object
Update nil attributes from given Hash or model.
Instance Method Details
#==(other) ⇒ Object
Default Model comparison
67 68 69 70 71 72 73 74 75 |
# File 'lib/ib/base_properties.rb', line 67 def == other case other when String # Probably a Rails URI, delegate to AR::Base super(other) else content_attributes.keys.inject(true) { |res, key| res && other.respond_to?(key) && (send(key) == other.send(key)) } end end |
#as_table(&b) ⇒ Object
└───────────┴─────────────────────────────────────────────┴─────┴────────┴─────────┴──────────┴────────────┴──────────┘
37 38 39 |
# File 'lib/ib/base_properties.rb', line 37 def as_table &b Terminal::Table.new headings: table_header(&b), rows: [table_row ], style: { border: :unicode } end |
#content_attributes ⇒ Object
Comparison support
42 43 44 45 46 47 48 49 |
# File 'lib/ib/base_properties.rb', line 42 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, :id, :order_id, :contract_id].include?(attr.to_sym) end] end |
#default_attributes ⇒ Object
Default attributes support
79 80 81 82 83 |
# File 'lib/ib/base_properties.rb', line 79 def default_attributes {:created_at => Time.now # :updated_at => Time.now, } end |
#invariant_attributes ⇒ Object
Remove all Time-Stamps from the list of Attributes
54 55 56 |
# File 'lib/ib/base_properties.rb', line 54 def invariant_attributes attributes.reject{|x| x =~ /_at/} end |
#set_attribute_defaults ⇒ Object
85 86 87 88 89 90 |
# File 'lib/ib/base_properties.rb', line 85 def set_attribute_defaults default_attributes.each do |key, val| self.send("#{key}=", val) if self.send(key).nil? # self.send("#{key}=", val) if self[key].nil? # Problems with association defaults end end |
#table_header ⇒ Object
20 21 22 |
# File 'lib/ib/base_properties.rb', line 20 def table_header [ self.class.to_s.demodulize ] + content_attributes.keys end |
#table_row ⇒ Object
23 24 25 |
# File 'lib/ib/base_properties.rb', line 23 def table_row [ self.class.to_s.demodulize ] + content_attributes.values end |
#to_human ⇒ Object
Default presentation
14 15 16 17 18 |
# File 'lib/ib/base_properties.rb', line 14 def to_human "<#{self.class.to_s.demodulize}: " + attributes.map do |attr, value| "#{attr}: #{value}" unless value.nil? end.compact.sort.join(' ') + ">" end |
#update_missing(attrs) ⇒ Object
Update nil attributes from given Hash or model
59 60 61 62 63 64 |
# File 'lib/ib/base_properties.rb', line 59 def update_missing attrs attrs = attrs.content_attributes unless attrs.kind_of?(Hash) attrs.each { |attr, val| send "#{attr}=", val if send(attr).blank? } self # for chaining end |