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

Instance Method Details

#==(other) ⇒ Object

Default Model comparison



46
47
48
49
50
51
52
53
54
# File 'lib/ib/base_properties.rb', line 46

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

#content_attributesObject

Comparison support



21
22
23
24
25
26
27
28
# File 'lib/ib/base_properties.rb', line 21

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_attributesObject

Default attributes support



58
59
60
61
62
# File 'lib/ib/base_properties.rb', line 58

def default_attributes
  {:created_at => Time.now
 #  :updated_at => Time.now,
   }
end

#invariant_attributesObject

Remove all Time-Stamps from the list of Attributes



33
34
35
# File 'lib/ib/base_properties.rb', line 33

def invariant_attributes
    attributes.reject{|x| x =~ /_at/}
end

#set_attribute_defaultsObject



64
65
66
67
68
69
# File 'lib/ib/base_properties.rb', line 64

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

#to_humanObject

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



38
39
40
41
42
43
# File 'lib/ib/base_properties.rb', line 38

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