Class: Tradier::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tradier/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Tradier::Base

Initializes a new object

Parameters:

  • attrs (Hash) (defaults to: {})


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

Parameters:



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

Parameters:

  • method (String, Symbol)

    Message to send to the object



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)

Parameters:

Returns:

  • (Boolean)


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

#attrsHash Also known as: to_hash

Retrieve the attributes of an object

Returns:

  • (Hash)


46
47
48
# File 'lib/tradier/base.rb', line 46

def attrs
  @attrs
end

#attrs_equal(other) ⇒ Boolean (protected)

Parameters:

Returns:

  • (Boolean)


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

Parameters:

  • attrs (Hash)

Returns:



55
56
57
58
# File 'lib/tradier/base.rb', line 55

def update(attrs)
  @attrs.update(attrs)
  self
end