Class: ApiSketch::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/api_sketch/dsl.rb

Defined Under Namespace

Classes: AttributeParser, Attributes, Base, ComplexAttributeParser, Headers, Parameters, Responses

Constant Summary collapse

COMPLEX_ATTRIBUTE_NAMES =
[:headers, :parameters, :responses]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definitions_dir = ) ⇒ DSL

Returns a new instance of DSL.



7
8
9
# File 'lib/api_sketch/dsl.rb', line 7

def initialize(definitions_dir=ApiSketch::Config[:definitions_dir])
  @definitions_dir = definitions_dir
end

Instance Attribute Details

#definitions_dirObject (readonly)

Returns the value of attribute definitions_dir.



3
4
5
# File 'lib/api_sketch/dsl.rb', line 3

def definitions_dir
  @definitions_dir
end

Instance Method Details

#init!Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/api_sketch/dsl.rb', line 11

def init!
  if File.directory?(config_dir)
    puts_info("Load configuration")
    load_dir_files(config_dir)
  end

  if File.directory?(resources_dir)
    puts_info("Load resources")
    load_dir_files(resources_dir)
  end
end

#resource(name, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/api_sketch/dsl.rb', line 27

def resource(name, &block)
  attributes = get_attrs(name, &block)

  COMPLEX_ATTRIBUTE_NAMES.each do |attribute_name|
    block_value = attributes[attribute_name]
    attributes[attribute_name] = get_complex_attribute(attribute_name, &block_value) if block_value
  end

  # Assign resource namespace
  attributes[:namespace] ||= block.source_location[0].gsub(resources_dir, "").gsub(".rb", "").split("/").reject { |ns| ns.nil? || ns == "" }.join("/")

  ::ApiSketch::Model::Resource.create(attributes)
end

#shared_block(name, &block) ⇒ Object



23
24
25
# File 'lib/api_sketch/dsl.rb', line 23

def shared_block(name, &block)
  ::ApiSketch::Model::SharedBlock.add(name, block)
end