Class: ActionAnnotation::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/action_annotation/utils.rb

Overview

Provides some methods that are needed at several locations in the plug-in.

Constant Summary collapse

PARSE_REGEXP =

Regexp for parsing description strings

/^([_\w]+)(.*\s([_\w]+))?(\s+(in|from|by)\s+(\S+))?$/

Class Method Summary collapse

Class Method Details

.get_tokens(description) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
# File 'lib/action_annotation/utils.rb', line 35

def self.get_tokens(description) # :nodoc:
  description = description.gsub(/\(.*\)/,'').strip
  #description = "shows all courses in @courses"
  matches = PARSE_REGEXP.match(description)
  raise ArgumentError, "'#{description}' could not be matches" unless matches
  [matches[1], matches[3], matches[6]]
end

.infinitive(verb) ⇒ Object

:nodoc:



45
46
47
48
# File 'lib/action_annotation/utils.rb', line 45

def self.infinitive(verb) # :nodoc:
  @infinitive_hash[verb] ||
      (verb.ends_with?("s") ? verb.first(-1) : verb)
end

.parse_description(description, allow_source = true) ⇒ Object

Parses a description string

  • description description of a controller action

  • allow_source if false, an exception is raised if the description contains a variable

Returns action, resource and source. See ActionAnnotation::Annotations::ClassMethods for details.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/action_annotation/utils.rb', line 19

def self.parse_description(description,allow_source=true)
  # description = "shows all courses in @courses (ignore this comment)"
  action, resource, source = get_tokens(description)
  # 'shows', 'courses', '@courses'
  if source
    raise ArgumentError, "Found unexpected source in '#{description}'" unless allow_source
    source = (source.last(-1)).to_sym if source.starts_with? ':'
  end
  returning Hash.new do |result|
    result[:action] = infinitive(action).to_sym
    result[:resource] = resource.singularize.to_sym if resource
    result[:source] = source if source
    # { :action => :show, :resource => :course, :source => '@courses' }
  end
end