Class: Angus::ResourceDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/angus/resource_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource_name, representations) ⇒ ResourceDefinition

Returns a new instance of ResourceDefinition.



6
7
8
9
10
# File 'lib/angus/resource_definition.rb', line 6

def initialize(resource_name, representations)
  @resource_name       = resource_name
  @resource_class_name = classify_resource(resource_name)
  @representations     = representations || {}
end

Instance Method Details

#build_response_metadata(response_representation) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/angus/resource_definition.rb', line 31

def (response_representation)
  return {} unless response_representation

  case response_representation
    when Angus::SDoc::Definitions::Representation
      result = []

      response_representation.fields.each do |field|
        result << (field)
      end

      result
    when Array
      result = []

      response_representation.each do |field|
        result << (field)
      end

      result
    else
      field_name = response_representation.name
      field_type_name = response_representation.type || response_representation.elements_type

      representation = representation_by_name(field_type_name)

      if representation.nil?
        field_name.to_sym
      else
        {field_name.to_sym => (representation)}
      end
  end
end

#canonical_nameObject



16
17
18
# File 'lib/angus/resource_definition.rb', line 16

def canonical_name
  Angus::String.underscore(@resource_class_name.to_s)
end

#operationsObject



12
13
14
# File 'lib/angus/resource_definition.rb', line 12

def operations
  @representations.operations[@resource_name.to_s] || []
end

#representation_by_name(name) ⇒ Object

TODO improve this find



66
67
68
# File 'lib/angus/resource_definition.rb', line 66

def representation_by_name(name)
  @representations.representations.find { |representation| representation.name == name }
end

#resource_classObject



20
21
22
23
24
25
# File 'lib/angus/resource_definition.rb', line 20

def resource_class
  return @resource_class if @resource_class
  require resource_path

  @resource_class = Object.const_get(@resource_class_name)
end

#resource_pathObject



27
28
29
# File 'lib/angus/resource_definition.rb', line 27

def resource_path
  File.join('resources', canonical_name)
end