Class: Angus::ResourceDefinition
- Inherits:
-
Object
- Object
- Angus::ResourceDefinition
- Defined in:
- lib/angus/resource_definition.rb
Instance Method Summary collapse
- #build_response_metadata(response_representation) ⇒ Object
- #canonical_name ⇒ Object
-
#initialize(root_path, resource_name, representations) ⇒ ResourceDefinition
constructor
A new instance of ResourceDefinition.
- #operations ⇒ Object
-
#representation_by_name(name) ⇒ Object
TODO improve this find.
- #resource_class ⇒ Object
- #resource_path ⇒ Object
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_name ⇒ Object
17 18 19 |
# File 'lib/angus/resource_definition.rb', line 17 def canonical_name Angus::String.underscore(@resource_class_name.to_s) end |
#operations ⇒ Object
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_class ⇒ Object
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_path ⇒ Object
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 |