Class: Seahorse::Operation
- Inherits:
-
Object
- Object
- Seahorse::Operation
- Defined in:
- lib/seahorse/operation.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#documentation ⇒ Object
Returns the value of attribute documentation.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#verb(verb = nil) ⇒ Object
readonly
Returns the value of attribute verb.
Instance Method Summary collapse
-
#initialize(controller, name, action = nil, &block) ⇒ Operation
constructor
A new instance of Operation.
- #input(type = nil, &block) ⇒ Object
- #output(type = nil, &block) ⇒ Object
- #to_hash ⇒ Object
- #url(url = nil) ⇒ Object
Constructor Details
#initialize(controller, name, action = nil, &block) ⇒ Operation
Returns a new instance of Operation.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/seahorse/operation.rb', line 6 def initialize(controller, name, action = nil, &block) @name = name.to_s @action = action.to_s @controller = controller url_prefix = "/" + controller.model_name.pluralize url_extra = nil case action.to_s when 'index' @verb = 'get' when 'show' @verb = 'get' url_extra = ':id' when 'destroy', 'delete' @verb = 'delete' url_extra = ':id' when 'create' @verb = 'post' when 'update' @verb = 'put' else @verb = 'get' url_extra = name.to_s end @url = url_prefix + (url_extra ? "/#{url_extra}" : "") instance_eval(&block) end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/seahorse/operation.rb', line 3 def action @action end |
#documentation ⇒ Object
Returns the value of attribute documentation.
4 5 6 |
# File 'lib/seahorse/operation.rb', line 4 def documentation @documentation end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/seahorse/operation.rb', line 3 def name @name end |
#verb(verb = nil) ⇒ Object (readonly)
Returns the value of attribute verb.
3 4 5 |
# File 'lib/seahorse/operation.rb', line 3 def verb @verb end |
Instance Method Details
#input(type = nil, &block) ⇒ Object
43 44 45 46 |
# File 'lib/seahorse/operation.rb', line 43 def input(type = nil, &block) @input ||= ShapeBuilder.type_class_for(type || 'structure').new type || block ? ShapeBuilder.new(@input).build(&block) : @input end |
#output(type = nil, &block) ⇒ Object
48 49 50 51 |
# File 'lib/seahorse/operation.rb', line 48 def output(type = nil, &block) @output ||= ShapeBuilder.type_class_for(type || 'structure').new type || block ? ShapeBuilder.new(@output).build(&block) : @output end |
#to_hash ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/seahorse/operation.rb', line 53 def to_hash { 'name' => name.camelcase, 'http' => { 'uri' => url.gsub(/:(\w+)/, '{\1}'), 'method' => verb.upcase }, 'input' => input.to_hash, 'output' => output.to_hash, 'documentation' => documentation } end |
#url(url = nil) ⇒ Object
39 40 41 |
# File 'lib/seahorse/operation.rb', line 39 def url(url = nil) url ? (@url = url) : @url end |