Class: ActiveApi::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/active_api/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Definition

Returns a new instance of Definition.



5
6
7
8
9
# File 'lib/active_api/definition.rb', line 5

def initialize(options)
  @definition_name  = options[:definition_name]
  @builder_class    = options[:builder_class] || ComplexType.to_s
  @fields           = options[:fields] || []
end

Instance Attribute Details

#builder_classObject (readonly)

Returns the value of attribute builder_class.



3
4
5
# File 'lib/active_api/definition.rb', line 3

def builder_class
  @builder_class
end

#definition_nameObject (readonly)

Returns the value of attribute definition_name.



3
4
5
# File 'lib/active_api/definition.rb', line 3

def definition_name
  @definition_name
end

#fieldsObject (readonly)

Returns the value of attribute fields.



3
4
5
# File 'lib/active_api/definition.rb', line 3

def fields
  @fields
end

Instance Method Details

#attribute(*args) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/active_api/definition.rb', line 11

def attribute(*args)
  options = args.extract_options!
  options[:type] ||= :string
  options.merge! :name => args.first, 
                 :field_type => :attribute
  field options
end

#attributesObject



51
52
53
54
55
# File 'lib/active_api/definition.rb', line 51

def attributes
  fields.select do |field|
    field.field_type == :attribute
  end
end

#belongs_to(name, options = {}) ⇒ Object



39
40
41
# File 'lib/active_api/definition.rb', line 39

def belongs_to(name, options = {})
  field options.merge(:name => name, :type => :belongs_to, :klass => ComplexType)
end

#element(name, type = :string, options = {}) ⇒ Object



19
20
21
# File 'lib/active_api/definition.rb', line 19

def element(name, type = :string, options = {})
  send type, name, options
end

#elementsObject



57
58
59
60
61
# File 'lib/active_api/definition.rb', line 57

def elements
  fields.select do |field|
    field.field_type == :element
  end
end

#has_many(name, options = {}) ⇒ Object



47
48
49
# File 'lib/active_api/definition.rb', line 47

def has_many(name, options = {})
  field options.merge(:name => name, :type => :has_many, :klass => Collection)
end

#has_one(name, options = {}) ⇒ Object



43
44
45
# File 'lib/active_api/definition.rb', line 43

def has_one(name, options = {})
  field options.merge(:name => name, :type => :has_one, :klass => ComplexType)
end

#plural_nameObject



67
68
69
# File 'lib/active_api/definition.rb', line 67

def plural_name
  name.to_s.pluralize
end

#singular_nameObject



63
64
65
# File 'lib/active_api/definition.rb', line 63

def singular_name
  name.to_s.singularize
end