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
-
#==(other) ⇒ Object
Default Model comparison.
-
#content_attributes ⇒ Object
Comparison support.
-
#default_attributes ⇒ Object
Default attributes support.
- #set_attribute_defaults ⇒ 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
41 42 43 44 45 46 47 48 49 |
# File 'lib/ib/base_properties.rb', line 41 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_attributes ⇒ Object
Comparison support
24 25 26 27 28 29 30 |
# File 'lib/ib/base_properties.rb', line 24 def content_attributes HashWithIndifferentAccess[attributes.reject do |(attr, _)| attr.to_s =~ /(_count)\z/ || [:created_at, :updated_at, :type, :id, :order_id, :contract_id].include?(attr.to_sym) end] end |
#default_attributes ⇒ Object
Default attributes support
53 54 55 56 57 |
# File 'lib/ib/base_properties.rb', line 53 def default_attributes {:created_at => Time.now, :updated_at => Time.now, } end |
#set_attribute_defaults ⇒ Object
59 60 61 62 63 64 |
# File 'lib/ib/base_properties.rb', line 59 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_human ⇒ Object
Default presentation
17 18 19 20 21 |
# File 'lib/ib/base_properties.rb', line 17 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
33 34 35 36 37 38 |
# File 'lib/ib/base_properties.rb', line 33 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 |