Class: Moonrope::DSL::ActionDSL
- Inherits:
-
Object
- Object
- Moonrope::DSL::ActionDSL
- Defined in:
- lib/moonrope/dsl/action_dsl.rb
Instance Method Summary collapse
-
#access_rule(name) ⇒ Object
Sets the name of the access rule to use for this action.
-
#action { ... } ⇒ void
Set the action to execute when this action is invoked.
-
#authenticator(name) ⇒ Object
Sets the name of the authenticator to use for this action.
-
#description(value) ⇒ void
Set the description for the action.
-
#error(name, description, options = {}) ⇒ void
Add a new error to the actions’ errors.
-
#filterable(&block) ⇒ Object
Specify that this action will return data which can be filtered by specifying certain parameters on a filter parameter.
-
#from_structure(name, &block) ⇒ Object
Specifies that all params within this block should be marked as being from a given structure.
-
#initialize(action) ⇒ ActionDSL
constructor
Initialize a new ActionDSL.
-
#no_doc! ⇒ Object
Set this action so that it isn’t documented.
-
#paginated(options = {}) ⇒ Object
Specify that this action will be returning paginated data.
-
#param(name, description_or_options = {}, options_if_description = {}, &block) ⇒ void
Add a new param to the action’s param set.
-
#returns(type, options = {}) ⇒ void
Sets the type of return value that is expected from a successful call to this API action.
-
#sortable(*fields) ⇒ Object
Specify that this action will return data sorted by user provided data.
-
#title(value) ⇒ void
Set the title for the action.
-
#use(name, options = {}) ⇒ Object
Include any block from the controller shares.
Constructor Details
#initialize(action) ⇒ ActionDSL
Initialize a new ActionDSL
13 14 15 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 13 def initialize(action) @action = action end |
Instance Method Details
#access_rule(name) ⇒ Object
Sets the name of the access rule to use for this action
140 141 142 143 144 145 146 147 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 140 def access_rule(name) if name.is_a?(Hash) authenticator name.first[0] access_rule name.first[1] else @action.access_rule = name end end |
#action { ... } ⇒ void
This method returns an undefined value.
Set the action to execute when this action is invoked.
action do
# Do something here and return a JSON-able value
end
159 160 161 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 159 def action(&block) @action.actions << block end |
#authenticator(name) ⇒ Object
Sets the name of the authenticator to use for this action
131 132 133 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 131 def authenticator(name) @action.authenticator = name end |
#description(value) ⇒ void
This method returns an undefined value.
Set the description for the action
description "Returns all users which are configured"
37 38 39 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 37 def description(value) @action.description = value end |
#error(name, description, options = {}) ⇒ void
This method returns an undefined value.
Add a new error to the actions’ errors
error "NoUnitFound", "The unit with given {{id}} could not be found"
@param description [String] a description of the error
108 109 110 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 108 def error(name, description, = {}) @action.errors[name] = .merge(:description => description, :from_share => @within_share) end |
#filterable(&block) ⇒ Object
Specify that this action will return data which can be filtered by specifying certain parameters on a filter parameter
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 196 def filterable(&block) if @action.errors['FilterError'].nil? error 'FilterError', "An error has occurred while processing filters for this action", :attributes => {:issue_code => "A more specific issue code", :issue_message => "A more specific message about the issue"} end if @action.params[:filters].nil? param :filters, "A hash of filters to apply to results", :type => Hash, :default => {} end dsl = FilterableDSL.new(@action) dsl.instance_eval(&block) end |
#from_structure(name, &block) ⇒ Object
Specifies that all params within this block should be marked as being from a given structure
from_structure :user do
param :username
end
92 93 94 95 96 97 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 92 def from_structure(name, &block) @from_structure = name self.instance_eval(&block) ensure @from_structure = nil end |
#no_doc! ⇒ Object
Set this action so that it isn’t documented
45 46 47 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 45 def no_doc! @action.doc = false end |
#paginated(options = {}) ⇒ Object
Specify that this action will be returning paginated data. Sets up the parameters for the action as appropriate.
167 168 169 170 171 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 167 def paginated( = {}) @action.traits << :paginated param :page, "The page number", :type => Integer, :required => true, :default => [:page] || 1 param :per_page, "The number of items to return per page", :type => Integer, :required => true, :default => [:per_page] || 30 end |
#param(name, description_or_options = {}, options_if_description = {}, &block) ⇒ void
This method returns an undefined value.
Add a new param to the action’s param set.
param :page, "The page number", :default => 2
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 59 def param(name, = {}, = {}, &block) if .is_a?(String) = .merge(:description => ) else = end [:from_structure] ||= @from_structure if @from_structure if structure = [:from_structure] if @action.controller && structure = @action.controller.base.structure(structure) if attribute = structure.attribute(name) [:description] ||= attribute.description [:type] ||= attribute.value_type end end end [:apply] = block if block_given? [:from_shared_action] = @within_shared_action.dup if @within_shared_action @action.params[name] = end |
#returns(type, options = {}) ⇒ void
This method returns an undefined value.
Sets the type of return value that is expected from a successful call to this API action.
returns :array, :structure => :user
122 123 124 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 122 def returns(type, = {}) @action.returns = .merge(:type => type) end |
#sortable(*fields) ⇒ Object
Specify that this action will return data sorted by user provided data.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 176 def sortable(*fields) if fields.empty? raise Moonrope::Errors::Error, "You must specify at least one field when calling 'sortable'" else if fields.first.is_a?(Hash) default_order = fields.first.first[1].to_s fields[0] = fields.first.first[0] else default_order = 'asc' end @action.traits << :sortable param :sort_by, "The field to sort by", :type => String, :required => true, :default => fields[0].to_s, :options => fields.map(&:to_s) param :order, "The direction to order units by", :type => String, :required => true, :default => default_order, :options => ["asc", "desc"] end end |
#title(value) ⇒ void
This method returns an undefined value.
Set the title for the action
title "List all users"
25 26 27 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 25 def title(value) @action.title = value end |
#use(name, options = {}) ⇒ Object
Include any block from the controller shares
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/moonrope/dsl/action_dsl.rb', line 211 def use(name, = {}) if block = (@action.controller.shared_actions[name] || @action.controller.base.shared_actions[name]) @within_shared_action ||= [] @within_shared_action << name self.instance_exec(, &block) else raise Moonrope::Errors::InvalidSharedAction, "Invalid share name #{name}" end ensure @within_shared_action.delete(name) if @within_shared_action end |