Module: Mameapns::Options

Included in:
Application, Notification
Defined in:
lib/mameapns/options.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/mameapns/options.rb', line 3

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#initialize(values = {}) ⇒ Object Also known as: initialize_options



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mameapns/options.rb', line 7

def initialize(values={})
  # like a sybolize_keys in active support
  values.keys.each { |k| values[k.to_sym] = values.delete(k) }
  
  self.class.attrs.each do |attr_name, option|
    if values.keys.include?(attr_name)
      self.__send__("#{attr_name}=", values[attr_name]) 
    else
      if option[:default]
        self.__send__("#{attr_name}=", option[:default])
      end
    end
  end
end

#to_hashObject



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

def to_hash
  hash = {}
  self.class.attrs.each do |attr_name, option|
    value = self.send(attr_name)
    if value.respond_to?(:to_hash)
      hash[attr_name.to_s] = value.to_hash
    else
      hash[attr_name.to_s] = value
    end
  end
  
  hash
end

#to_jsonObject



36
37
38
# File 'lib/mameapns/options.rb', line 36

def to_json
  to_hash.to_json
end