Class: Monty::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/monty/resource.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • name (String, Symbol)

    resource reference.



34
35
36
37
38
# File 'lib/monty/resource.rb', line 34

def initialize(name)
  @name = name.to_s
  @regex_pattern = "\/#{@name}(\/.*)?"
  self.class.register_regex(@regex_pattern)
end

Class Attribute Details

.resourcesObject

Returns the value of attribute resources.



6
7
8
# File 'lib/monty/resource.rb', line 6

def resources
  @resources
end

.resources_regexObject

Returns the value of attribute resources_regex.



6
7
8
# File 'lib/monty/resource.rb', line 6

def resources_regex
  @resources_regex
end

Instance Attribute Details

#exceptionsObject

The only methods restricted on the resource



28
29
30
# File 'lib/monty/resource.rb', line 28

def exceptions
  @exceptions
end

#inclusionsObject

The only methods allowed on the resource



30
31
32
# File 'lib/monty/resource.rb', line 30

def inclusions
  @inclusions
end

#nameObject

Name of the resource



24
25
26
# File 'lib/monty/resource.rb', line 24

def name
  @name
end

#regex_patternObject

Regular expression pattern



26
27
28
# File 'lib/monty/resource.rb', line 26

def regex_pattern
  @regex_pattern
end

Class Method Details

.regexRegexp

Returns created from resources’ base regex.

Returns:

  • (Regexp)

    created from resources’ base regex



15
16
17
# File 'lib/monty/resource.rb', line 15

def regex
  @resources_regex ||= Monty.regex(@resources.join("|"))
end

.register_regex(resource) ⇒ Object

When a new resource is created, this method is called to register the root



9
10
11
12
# File 'lib/monty/resource.rb', line 9

def register_regex(resource)
  resource = "(#{resource})"
  @resources << resource unless @resources.include?(resource)
end

Instance Method Details

#except(*methods) ⇒ Object

Parameters:

  • * (String, Symbol)

    only methods restricted on the resource



41
42
43
44
45
# File 'lib/monty/resource.rb', line 41

def except(*methods)
  return if methods.empty?
  @exceptions = methods.collect{|m| m.to_s}
  @regex_pattern = "\/#{@name}(?!\/(#{@exceptions.join('|')}))(\/.*)?"
end

#only(*methods) ⇒ Object

Parameters:

  • * (String, Symbol)

    only methods allowed on the resource



48
49
50
51
52
# File 'lib/monty/resource.rb', line 48

def only(*methods)
  return if methods.empty?
  @inclusions = methods.collect{|m| m.to_s}
  @regex_pattern = "\/#{@name}\/(#{@inclusions.join('|')})(\/)?"
end