Class: SwaggerHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/swagger_hash.rb

Constant Summary collapse

KEEP_METHODS =
%w{default []= each merge! debugger puts __id__ __send__ instance_eval == equal? initialize delegate caller object_id raise class [] to_json inspect to_s new nil?}

Instance Method Summary collapse

Constructor Details

#initializeSwaggerHash

Returns a new instance of SwaggerHash.



7
8
9
10
# File 'lib/swagger_hash.rb', line 7

def initialize
  @namespaces = Hash.new
  ##super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/swagger_hash.rb', line 20

def method_missing(method, *args, &block)

  if method.to_s.match(/=$/)
    self[method.to_s.gsub("=","").to_sym] = args.first
  else
    if self[method].nil?
      if not method == :add
        self[method] = SwaggerHash.new
      else
        if (args.nil? || args.empty?)
          self[:_array] ||= Array.new 
          item = SwaggerHash.new
          self[:_array] << item
          return item
        else
          self[:_array] ||= Array.new
          args.each do |item|
            self[:_array] << item
          end
        end
      end
    end
    return self[method]
  end
end

Instance Method Details

#namespace(name) ⇒ Object



12
13
14
# File 'lib/swagger_hash.rb', line 12

def namespace(name)
  @namespaces[name] ||= SwaggerHash.new
end

#namespacesObject



16
17
18
# File 'lib/swagger_hash.rb', line 16

def namespaces
  @namespaces
end

#set(*args) ⇒ Object



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

def set(*args)
  merge!(*args)  
end

#to_hashObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/swagger_hash.rb', line 50

def to_hash
  h2 = Hash.new
  self.each do |k,v|
    if v.class != SwaggerHash && v.class != Hash
      h2[k] = v 
    else
      if not (v[:_array].nil?)
        v2 = []
        v[:_array].each do |item|
          v2 << item.to_hash
        end
        h2[k] = v2
      else
        h2[k] = v.to_hash
      end
    end
  end
  return h2
end