Class: ApiTwister::ApiDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/api-twister/api_definition.rb

Constant Summary collapse

TYPE_MAP =
{:association => ApiAssociation, :method => ApiMethod, :attribute => ApiAttribute, :code_table => CodeTable}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options = {}) ⇒ ApiDefinition

Returns a new instance of ApiDefinition.



24
25
26
27
28
29
# File 'lib/api-twister/api_definition.rb', line 24

def initialize(model, options = {})
  @has_attributes = false
  @model = model
  @api_items = {}
  @node_name = options[:node_name] || model.name.underscore.dasherize
end

Instance Attribute Details

#api_itemsObject (readonly)

Returns the value of attribute api_items.



3
4
5
# File 'lib/api-twister/api_definition.rb', line 3

def api_items
  @api_items
end

#modelObject

Returns the value of attribute model.



4
5
6
# File 'lib/api-twister/api_definition.rb', line 4

def model
  @model
end

#node_nameObject

Returns the value of attribute node_name.



4
5
6
# File 'lib/api-twister/api_definition.rb', line 4

def node_name
  @node_name
end

Instance Method Details

#all_items(type = nil) ⇒ Object

Returns a list of symbols where the symbol is the name of the item



8
9
10
11
12
13
14
# File 'lib/api-twister/api_definition.rb', line 8

def all_items(type = nil)
  if type
    @api_items.select {|k, v| v.kind_of?(TYPE_MAP[type])}.keys
  else
    @api_items.keys
  end
end

#all_objects(type = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/api-twister/api_definition.rb', line 16

def all_objects(type = nil)
  if type
    @api_items.select {|k, v| v.kind_of?(TYPE_MAP[type])}.values
  else
    @api_items.values
  end
end

#association(assoc) ⇒ Object



31
32
33
# File 'lib/api-twister/api_definition.rb', line 31

def association(assoc)
  @api_items[assoc] = ApiAssociation.new(model, assoc)
end

#associations(*assocs) ⇒ Object



35
36
37
# File 'lib/api-twister/api_definition.rb', line 35

def associations(*assocs)
  assocs.each {|a| association a}
end

#attribute(attrib, data_type = "string", description = nil, required = false) ⇒ Object



39
40
41
42
# File 'lib/api-twister/api_definition.rb', line 39

def attribute(attrib, data_type = "string", description = nil, required = false)
  @has_attributes = true
  @api_items[attrib] = ApiAttribute.new(attrib, data_type, description, required)
end

#attributes(*attribs) ⇒ Object



44
45
46
# File 'lib/api-twister/api_definition.rb', line 44

def attributes(*attribs) 
  attribs.each {|a| attribute a }
end

#code_table(name, table_name, order_by = nil) ⇒ Object



58
59
60
# File 'lib/api-twister/api_definition.rb', line 58

def code_table(name, table_name, order_by=nil)
  @api_items[name] = CodeTable.new(name, table_name, order_by)
end

#has_attributes?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/api-twister/api_definition.rb', line 62

def has_attributes?
  @has_attributes
end

#method(meth, data_type = "string", description = nil, required = false) ⇒ Object



49
50
51
# File 'lib/api-twister/api_definition.rb', line 49

def method(meth, data_type = "string", description = nil, required = false)
  @api_items[meth] = ApiMethod.new(meth, data_type, description, required)
end

#methods(*meths) ⇒ Object



54
55
56
# File 'lib/api-twister/api_definition.rb', line 54

def methods(*meths)
  meths.each {|m| method m}
end

#orig_methodObject



48
# File 'lib/api-twister/api_definition.rb', line 48

alias_method :orig_method, :method

#orig_methodsObject



53
# File 'lib/api-twister/api_definition.rb', line 53

alias_method :orig_methods, :methods