Class: MtGox::Models::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/mtgox/models/model.rb

Overview

Base data Model class, in future it will be developed into ActiveModel

Direct Known Subclasses

Balance, Depth, MultiTicker, Offer, Ticker

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Model

Returns a new instance of Model.



47
48
49
50
# File 'lib/mtgox/models/model.rb', line 47

def initialize attrs={}
  @attributes = {}
  set_attributes attrs
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



45
46
47
# File 'lib/mtgox/models/model.rb', line 45

def attributes
  @attributes
end

Class Method Details

.define_property(names, type) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mtgox/models/model.rb', line 14

def self.define_property names, type
  aliases = [names].flatten
  name = aliases.shift
  instance_eval do

    define_method(name) do
      @attributes[name]
    end

    case type
      when ''
        define_method("#{name}=") do |value|
          @attributes[name] = value
        end
      when Proc
        define_method("#{name}=") do |value|
          @attributes[name] = type.call(value)
        end
      else
        define_method("#{name}=") do |value|
          @attributes[name] = value.send "to_#{type}"
        end
    end

    aliases.each do |ali|
      alias_method "#{ali}", name
      alias_method "#{ali}=", "#{name}="
    end
  end
end

.prop(*properties) ⇒ Object



7
8
9
10
11
12
# File 'lib/mtgox/models/model.rb', line 7

def self.prop *properties
  prop_hash = properties.last.is_a?(Hash) ? properties.pop : {}

  properties.each { |names| define_property names, '' }
  prop_hash.each { |names, type| define_property names, type }
end

Instance Method Details

#set_attributes(attrs = {}) ⇒ Object



52
53
54
# File 'lib/mtgox/models/model.rb', line 52

def set_attributes attrs={}
  attrs.keys.each { |key| self.send("#{key}=", attrs[key]) }
end