Class: Cielo24::BaseOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/cielo24/options.rb

Instance Method Summary collapse

Instance Method Details

#get_hashObject



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

def get_hash
  hash = {}
  self.instance_variables.each{ |var|
    value = self.instance_variable_get(var)
    unless value.nil?
      hash[var.to_s.delete('@')] = value
    end
  }
  return hash
end

#populate_from_hash(hash) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/cielo24/options.rb', line 35

def populate_from_hash(hash)
  unless hash.nil?
    hash.each do |key, value|
      populate_from_key_value_pair(key, value)
    end
  end
end

#populate_from_key_value_pair(key, value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/cielo24/options.rb', line 24

def populate_from_key_value_pair(key, value)
  # Iterate over instance variables
  self.instance_variables.each{ |var|
    # key gets converted to_s because it can be a Symbol
    if var.to_s.delete('@') == key.to_s
      self.instance_variable_set(var, value)
      break
    end
  }
end

#to_queryObject



15
16
17
18
19
20
21
22
# File 'lib/cielo24/options.rb', line 15

def to_query
  hash = get_hash
  array = Array.new
  hash.each do |key, value|
    array.push(key + '=' + value.to_s)
  end
  return array.join('&')
end