Module: Moonrope::EvalHelpers

Included in:
EvalEnvironment
Defined in:
lib/moonrope/eval_helpers.rb,
lib/moonrope/eval_helpers/filter_helper.rb

Defined Under Namespace

Modules: FilterHelper

Instance Method Summary collapse

Instance Method Details

#error(type, code_or_message = nil, message = nil) ⇒ Object

Raise an error.

Parameters:

  • type (Symbol)

    the type of error to raise

  • message (String, Hash or Array) (defaults to: nil)

    options to pass with the error (usually a message)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/moonrope/eval_helpers.rb', line 10

def error(type, code_or_message = nil, message = nil)
  case type
  when :not_found           then raise(Moonrope::Errors::NotFound, code_or_message)
  when :access_denied       then raise(Moonrope::Errors::AccessDenied, code_or_message)
  when :validation_error    then raise(Moonrope::Errors::ValidationError, code_or_message)
  when :parameter_error     then raise(Moonrope::Errors::ParameterError, code_or_message)
  when :structured_error    then structured_error(code_or_message, message)
  else
    if type.is_a?(String)
      if code_or_message.is_a?(Hash)
        structured_error(type, nil, code_or_message)
      else
        structured_error(type, code_or_message, message.is_a?(Hash) ? message : {})
      end
    else
      raise Moonrope::Errors::RequestError, code_or_message
    end
  end
end

#paginate(collection, max_per_page = 60, &block) ⇒ Object

Return paginated information



54
55
56
57
58
59
60
61
62
# File 'lib/moonrope/eval_helpers.rb', line 54

def paginate(collection, max_per_page = 60, &block)
  per_page = params.per_page || 30
  per_page = max_per_page if per_page < 1 || per_page > max_per_page
  paginated_results = collection.page(params.page || 1).per(per_page)
  set_flag :paginated, {:page => params.page || 1, :per_page => per_page, :total_pages => paginated_results.total_pages, :total_records => paginated_results.total_count}
  paginated_results.to_a.map do |result|
    block.call(result)
  end
end

#sort(collection, &block) ⇒ Object

Return information sorted by appropriate values from parameters



67
68
69
70
71
# File 'lib/moonrope/eval_helpers.rb', line 67

def sort(collection, &block)
  collection = collection.order(params.sort_by => params.order)
  set_flag :sorted, {:by => params.sort_by, :order => params.order}
  block.call(collection)
end

#structured_error(code, message, additional = {}) ⇒ Object

 Raises a structured error.

Parameters:

  • code (String)

    the code to return

  • message (String)

    explantory text to return

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

    additional data to return with the error

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/moonrope/eval_helpers.rb', line 37

def structured_error(code, message, additional = {})
  if action
    if action.authenticator_to_use.is_a?(Moonrope::Authenticator)
      errors = action.authenticator_to_use.errors.merge(action.errors)
    else
      errors = action.errors
    end
    if error = errors[code]
      message = error[:description].gsub(/\{(\w+)\}/) { additional[$1.to_sym] }
    end
  end
  raise Moonrope::Errors::StructuredError, {:code => code, :message => message}.merge(additional)
end