Module: IB::BaseProperties

Extended by:
ActiveSupport::Concern
Included in:
Bar, ComboLeg, Contract, ContractDetail, Execution, Order, OrderState, 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



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

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
# File 'lib/ib/base_properties.rb', line 21

def content_attributes
  HashWithIndifferentAccess[attributes.reject do |(attr, _)|
                              attr.to_s =~ /(_count)$/ ||
                                [:created_at, :updated_at, :type,
                                 :id, :order_id, :contract_id].include?(attr.to_sym)
  end]
end

#default_attributesObject

Default attributes support



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

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

#set_attribute_defaultsObject



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

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



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

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