Class: ApiSpec::APIObject

Inherits:
Object
  • Object
show all
Defined in:
lib/api_spec/lib/api_spec/generator.rb

Direct Known Subclasses

Endpoint, Method, Parameter, Spec

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ APIObject

Returns a new instance of APIObject.



15
16
17
18
# File 'lib/api_spec/lib/api_spec/generator.rb', line 15

def initialize(name)
  @name = name
  send("#{nested_name}=".to_sym, []) if nested_name
end

Class Attribute Details

.nested_adder_nameObject

Returns the value of attribute nested_adder_name.



4
5
6
# File 'lib/api_spec/lib/api_spec/generator.rb', line 4

def nested_adder_name
  @nested_adder_name
end

.nested_classObject

Returns the value of attribute nested_class.



4
5
6
# File 'lib/api_spec/lib/api_spec/generator.rb', line 4

def nested_class
  @nested_class
end

.nested_nameObject

Returns the value of attribute nested_name.



4
5
6
# File 'lib/api_spec/lib/api_spec/generator.rb', line 4

def nested_name
  @nested_name
end

Class Method Details

.init(base) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/api_spec/lib/api_spec/generator.rb', line 34

def self.init(base)
  base.send(:attr_accessor, *base::JSON_NAMES.keys)
  base.send(:attr_accessor, base.nested_name) if base.nested_name
  base.send(:define_method, base.nested_adder_name) do |name, &block|
    obj = nested_class.new(name)
    block.call(obj)
    send(nested_name) << obj
  end if base.nested_adder_name
end

Instance Method Details

#nested_classObject



11
12
13
# File 'lib/api_spec/lib/api_spec/generator.rb', line 11

def nested_class
  self.class.nested_class
end

#nested_nameObject



7
8
9
# File 'lib/api_spec/lib/api_spec/generator.rb', line 7

def nested_name
  self.class.nested_name
end

#to_hashObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/api_spec/lib/api_spec/generator.rb', line 20

def to_hash
  pairs = self.class::JSON_NAMES.map do |name, json_name|
    [json_name, send(name)]
  end

  hash = Hash[pairs]

  if nested_name
    hash[nested_name.to_s] = send(nested_name).map { |obj| obj.to_hash }
  end

  hash
end