Class: Moonrope::DSL::ActionDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/moonrope/dsl/action_dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ ActionDSL

Initialize a new ActionDSL

Parameters:



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

Parameters:

  • name (Symbol)

    the name of the authenticator



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

Yields:

  • the contents of the yield will be saved as the action



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

Parameters:

  • name (Symbol)

    the name of the authenticator



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"

Parameters:

  • value (String)


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

Parameters:

  • name (String)

    the name of the error



108
109
110
# File 'lib/moonrope/dsl/action_dsl.rb', line 108

def error(name, description, options = {})
  @action.errors[name] = options.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

Parameters:

  • name (Symbol)

    the name of the structure



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(options = {})
  @action.traits << :paginated
  param :page, "The page number", :type => Integer, :required => true, :default => options[:page] || 1
  param :per_page, "The number of items to return per page", :type => Integer, :required => true, :default => options[: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

Parameters:

  • name (Symbol)

    the name of the param

  • description_or_options (String/Hash) (defaults to: {})

    a description of the action or options

  • options_if_description (Hash) (defaults to: {})

    a hash of additional options if a description was provided



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, description_or_options = {}, options_if_description = {}, &block)
  if description_or_options.is_a?(String)
    options = options_if_description.merge(:description => description_or_options)
  else
    options = description_or_options
  end

  options[:from_structure] ||= @from_structure if @from_structure

  if structure = options[:from_structure]
    if @action.controller && structure = @action.controller.base.structure(structure)
      if attribute = structure.attribute(name)
        options[:description] ||= attribute.description
        options[:type] ||= attribute.value_type
      end
    end
  end

  options[:apply] = block if block_given?
  options[:from_shared_action] = @within_shared_action.dup if @within_shared_action
  @action.params[name] = options
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

Parameters:

  • type (Symbol)

    the type of object that will be returend

  • options (Hash) (defaults to: {})

    further options about the returned value



122
123
124
# File 'lib/moonrope/dsl/action_dsl.rb', line 122

def returns(type, options = {})
  @action.returns = options.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"

Parameters:

  • value (String)


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, options = {})
  if block = (@action.controller.shared_actions[name] || @action.controller.base.shared_actions[name])
    @within_shared_action ||= []
    @within_shared_action << name
    self.instance_exec(options, &block)
  else
    raise Moonrope::Errors::InvalidSharedAction, "Invalid share name #{name}"
  end
ensure
  @within_shared_action.delete(name) if @within_shared_action
end