Class: Swagger::Blocks::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger/blocks.rb

Overview

Base node for representing every object in the Swagger DSL.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name.



129
130
131
# File 'lib/swagger/blocks.rb', line 129

def name
  @name
end

Class Method Details

.call(options = {}, &block) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/swagger/blocks.rb', line 131

def self.call(options = {}, &block)
  # Create a new instance and evaluate the block into it.
  instance = new
  instance.name = options[:name] if options[:name]
  instance.instance_eval(&block)
  instance
end

Instance Method Details

#as_jsonObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/swagger/blocks.rb', line 139

def as_json
  result = {}
  self.data.each do |key, value|
    if value.is_a?(Node)
      result[key] = value.as_json
    elsif value.is_a?(Array)
      result[key] = []
      value.each { |v| result[key] << (v.respond_to?(:as_json) ? v.as_json : v) }
    else
      result[key] = value
    end
  end
  return result if !name
  # If "name" is given to this node, wrap the data with a root element with the given name.
  {name => result}
end

#dataObject



156
157
158
# File 'lib/swagger/blocks.rb', line 156

def data
  @data ||= {}
end

#key(key, value) ⇒ Object



160
161
162
# File 'lib/swagger/blocks.rb', line 160

def key(key, value)
  self.data[key] = value
end