Class: Tradier::Base
- Inherits:
-
Object
- Object
- Tradier::Base
- Defined in:
- lib/tradier/base.rb
Direct Known Subclasses
Account, Adjustment, Balance, Calendar, Clock, Event, EventSession, Expiration, History, OptionQuote, Order, Position, Profile, Quote, Security, Timesales, Watchlist, WatchlistItem
Class Method Summary collapse
-
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key.
- .from_response(body = {}) ⇒ Object
Instance Method Summary collapse
-
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation.
- #attr_equal(attr, other) ⇒ Boolean protected
-
#attrs ⇒ Hash
(also: #to_hash)
Retrieve the attributes of an object.
- #attrs_equal(other) ⇒ Boolean protected
-
#initialize(attrs = {}) ⇒ Tradier::Base
constructor
Initializes a new object.
-
#update(attrs) ⇒ Tradier::Base
Update the attributes of an object.
Constructor Details
#initialize(attrs = {}) ⇒ Tradier::Base
Initializes a new object
30 31 32 |
# File 'lib/tradier/base.rb', line 30 def initialize(attrs={}) @attrs = attrs end |
Class Method Details
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tradier/base.rb', line 11 def self.attr_reader(*attrs) mod = Module.new do attrs.each do |attribute| define_method attribute do @attrs[attribute.to_sym] end define_method "#{attribute}?" do !!@attrs[attribute.to_sym] end end end const_set(:Attributes, mod) include mod end |
.from_response(body = {}) ⇒ Object
4 5 6 |
# File 'lib/tradier/base.rb', line 4 def self.from_response(body={}) new(body) end |
Instance Method Details
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation
37 38 39 40 41 |
# File 'lib/tradier/base.rb', line 37 def [](method) send(method.to_sym) rescue NoMethodError nil end |
#attr_equal(attr, other) ⇒ Boolean (protected)
65 66 67 |
# File 'lib/tradier/base.rb', line 65 def attr_equal(attr, other) self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr) end |
#attrs ⇒ Hash Also known as: to_hash
Retrieve the attributes of an object
46 47 48 |
# File 'lib/tradier/base.rb', line 46 def attrs @attrs end |
#attrs_equal(other) ⇒ Boolean (protected)
71 72 73 |
# File 'lib/tradier/base.rb', line 71 def attrs_equal(other) self.class == other.class && !other.attrs.empty? && attrs == other.attrs end |
#update(attrs) ⇒ Tradier::Base
Update the attributes of an object
55 56 57 58 |
# File 'lib/tradier/base.rb', line 55 def update(attrs) @attrs.update(attrs) self end |