Class: Angus::ResourceDefinition

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

Instance Method Summary collapse

Constructor Details

#initialize(root_path, resource_name, representations) ⇒ ResourceDefinition

Returns a new instance of ResourceDefinition.



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

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

Instance Method Details

#build_response_metadata(response_representation) ⇒ Object



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
64
65
66
67
68
69
70
# File 'lib/angus/resource_definition.rb', line 38

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



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

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

#operationsObject



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

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

#representation_by_name(name) ⇒ Object

TODO improve this find



73
74
75
# File 'lib/angus/resource_definition.rb', line 73

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

#resource_classObject



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

def resource_class
  return @resource_class if @resource_class
  require resource_path

  @resource_class = Object.const_get(@resource_class_name)
end

#resource_pathObject



28
29
30
31
32
33
34
35
36
# File 'lib/angus/resource_definition.rb', line 28

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

  if @root_path.empty?
    resource_path
  else
    File.join(@root_path, resource_path)
  end
end