Class: IGMarkets::Model
- Inherits:
-
Object
- Object
- IGMarkets::Model
- Defined in:
- lib/ig_markets/model.rb,
lib/ig_markets/model/typecasters.rb
Overview
Implement typecaster methods for Model.
Direct Known Subclasses
Account, Account::Balance, Activity, Application, ClientSentiment, DealConfirmation, DealConfirmation::AffectedDeal, DealingPlatform::SprintMarketPositionMethods::SprintMarketPositionCreateAttributes, HistoricalPriceResult, HistoricalPriceResult::DataAllowance, HistoricalPriceResult::Price, HistoricalPriceResult::Snapshot, Instrument, Instrument::Currency, Instrument::ExpiryDetails, Instrument::MarginDepositBand, Instrument::OpeningHours, Instrument::RolloverDetails, Instrument::SlippageFactor, Market, IGMarkets::Market::DealingRules, IGMarkets::Market::DealingRules::RuleDetails, IGMarkets::Market::Snapshot, MarketHierarchyResult, IGMarkets::MarketHierarchyResult::HierarchyNode, MarketOverview, Position, SprintMarketPosition, Transaction, Watchlist, WorkingOrder
Class Attribute Summary collapse
-
.defined_attributes ⇒ Hash
A hash containing details of all attributes that have been defined on this model.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
The current attribute values set on this model.
Class Method Summary collapse
-
.allowed_values(attribute_name) ⇒ Array
Returns the array of allowed values for the specified attribute that was passed to Model.attribute.
-
.attribute(name, type = String, options = {}) ⇒ Object
Defines setter and getter methods for a new attribute on this class.
-
.attribute_type(attribute_name) ⇒ Object
Returns the type of the specified attribute.
-
.defined_attribute_names ⇒ Array<Symbol>
Returns the names of all currently defined attributes for this model.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares this model to another, the attributes and class must match for them to be considered equal.
-
#initialize(attributes = {}) ⇒ Model
constructor
Initializes this new model with the given attribute values.
-
#initialize_copy(other) ⇒ Object
Copy initializer that duplicates the #attributes hash in full.
-
#inspect ⇒ String
Returns a human-readable string containing this model’s type and all its current attribute values.
Constructor Details
#initialize(attributes = {}) ⇒ Model
Initializes this new model with the given attribute values. Attributes not known to this model will raise ArgumentError.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ig_markets/model.rb', line 13 def initialize(attributes = {}) defined_attribute_names = self.class.defined_attribute_names defined_attribute_names.each do |name| send "#{name}=", attributes[name] end (attributes.keys - defined_attribute_names).map do |attribute| value = attributes[attribute] raise ArgumentError, "unknown attribute: #{self.class.name}##{attribute}, value: #{inspect_value value}" end end |
Class Attribute Details
.defined_attributes ⇒ Hash
Returns A hash containing details of all attributes that have been defined on this model.
71 72 73 |
# File 'lib/ig_markets/model.rb', line 71 def defined_attributes @defined_attributes end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns The current attribute values set on this model.
7 8 9 |
# File 'lib/ig_markets/model.rb', line 7 def attributes @attributes end |
Class Method Details
.allowed_values(attribute_name) ⇒ Array
Returns the array of allowed values for the specified attribute that was passed to attribute.
94 95 96 |
# File 'lib/ig_markets/model.rb', line 94 def allowed_values(attribute_name) defined_attributes.fetch(attribute_name).fetch :allowed_values end |
.attribute(name, type = String, options = {}) ⇒ Object
Defines setter and getter methods for a new attribute on this class.
118 119 120 121 122 123 124 |
# File 'lib/ig_markets/model.rb', line 118 def attribute(name, type = String, = {}) define_attribute_reader name define_attribute_writer name, type, self.defined_attributes ||= {} self.defined_attributes[name] = .merge type: type end |
.attribute_type(attribute_name) ⇒ Object
Returns the type of the specified attribute.
85 86 87 |
# File 'lib/ig_markets/model.rb', line 85 def attribute_type(attribute_name) defined_attributes.fetch(attribute_name).fetch :type end |
.defined_attribute_names ⇒ Array<Symbol>
Returns the names of all currently defined attributes for this model.
76 77 78 |
# File 'lib/ig_markets/model.rb', line 76 def defined_attribute_names (defined_attributes || {}).keys end |
Instance Method Details
#==(other) ⇒ Boolean
Compares this model to another, the attributes and class must match for them to be considered equal.
41 42 43 |
# File 'lib/ig_markets/model.rb', line 41 def ==(other) self.class == other.class && attributes == other.attributes end |
#initialize_copy(other) ⇒ Object
Copy initializer that duplicates the #attributes hash in full.
30 31 32 33 34 |
# File 'lib/ig_markets/model.rb', line 30 def initialize_copy(other) super @attributes = other.attributes.dup end |
#inspect ⇒ String
Returns a human-readable string containing this model’s type and all its current attribute values.
48 49 50 51 52 53 54 |
# File 'lib/ig_markets/model.rb', line 48 def inspect formatted_attributes = self.class.defined_attribute_names.map do |attribute| "#{attribute}: #{inspect_value send(attribute)}" end "#<#{self.class.name} #{formatted_attributes.join ', '}>" end |