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, AccountActivity, AccountTransaction, 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, 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.
-
.defined_attribute_names ⇒ Array<Symbol>
Returns the names of all currently defined attributes for this model.
-
.from(source) ⇒ nil, ...
Creates a new Model instance from the specified source, which can take a variety of different forms.
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.
-
#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.
62 63 64 |
# File 'lib/ig_markets/model.rb', line 62 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.
76 77 78 |
# File 'lib/ig_markets/model.rb', line 76 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.
98 99 100 101 102 103 104 |
# File 'lib/ig_markets/model.rb', line 98 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 |
.defined_attribute_names ⇒ Array<Symbol>
Returns the names of all currently defined attributes for this model.
67 68 69 |
# File 'lib/ig_markets/model.rb', line 67 def defined_attribute_names (defined_attributes || {}).keys end |
.from(source) ⇒ nil, ...
Creates a new Model instance from the specified source, which can take a variety of different forms.
115 116 117 118 119 120 121 122 |
# File 'lib/ig_markets/model.rb', line 115 def from(source) return nil if source.nil? return new source if source.is_a? Hash return source.dup if source.is_a? self return source.map { |item| from item } if source.is_a? Array raise ArgumentError, "Unable to make a #{self} from instance of #{source.class}" end |
Instance Method Details
#==(other) ⇒ Boolean
Compares this model to another, the attributes and class must match for them to be considered equal.
32 33 34 |
# File 'lib/ig_markets/model.rb', line 32 def ==(other) self.class == other.class && attributes == other.attributes end |
#inspect ⇒ String
Returns a human-readable string containing this model’s type and all its current attribute values.
39 40 41 42 43 44 45 |
# File 'lib/ig_markets/model.rb', line 39 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 |