Class: Bitfinex::Models::Model

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, fields, boolFields) ⇒ Model

Returns a new instance of Model.



4
5
6
7
8
9
10
11
12
13
# File 'lib/models/model.rb', line 4

def initialize (data, fields, boolFields)
  @fields = fields
  @boolFields = boolFields

  if data.kind_of?(Array)
    apply(self.class.unserialize(data))
  elsif data.kind_of?(Hash)
    apply(data)
  end
end

Class Method Details

.unserialize(data, fields, boolFields) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/models/model.rb', line 39

def self.unserialize (data, fields, boolFields)
  obj = {}

  fields.each do |key, index|
    return if index.nil?

    if boolFields.include?(key)
      obj[key] = data[index] == 1
    else
      obj[key] = data[index]
    end
  end

  return obj
end

Instance Method Details

#apply(obj) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/models/model.rb', line 31

def apply (obj)
  @fields.each do |key, index|
    return if index.nil?

    instance_variable_set("@#{key}", obj[key])
  end
end

#serializeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/models/model.rb', line 15

def serialize
  arr = []

  @fields.each do |key, index|
    return if index.nil?

    if @boolFields.include?(key)
      arr[index] = instance_variable_get("@#{key}") ? 1 : 0
    else
      arr[index] = instance_variable_get("@#{key}")
    end
  end

  arr
end