Class: AnotherApi::Scope

Inherits:
Data
  • Object
show all
Defined in:
lib/another_api/scope.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group:, action: :all) ⇒ Scope

Returns a new instance of Scope.

Raises:

  • (ArgumentError)


5
6
7
8
# File 'lib/another_api/scope.rb', line 5

def initialize(group:, action: :all)
  raise ArgumentError, "Invalid action: #{action}" unless SCOPE_ACTIONS.include?(action.to_sym)
  super(group: group.to_sym, action: action.to_sym)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



4
5
6
# File 'lib/another_api/scope.rb', line 4

def action
  @action
end

#groupObject (readonly)

Returns the value of attribute group

Returns:

  • (Object)

    the current value of group



4
5
6
# File 'lib/another_api/scope.rb', line 4

def group
  @group
end

Class Method Details

.parse(scope_str, prefix: AnotherApi.configuration.scope_prefix) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
# File 'lib/another_api/scope.rb', line 10

def self.parse(scope_str, prefix: AnotherApi.configuration.scope_prefix)
  stripped = scope_str.to_s.delete_prefix(prefix)
  parts = stripped.split(".")
  raise ArgumentError, "Invalid scope string: #{scope_str}" if parts.size < 2
  action = parts.pop
  group = parts.join(".")
  new(group: group.to_sym, action: action.to_sym)
end

Instance Method Details

#inspectObject



27
28
29
# File 'lib/another_api/scope.rb', line 27

def inspect
  "#<AnotherApi::Scope #{group}.#{action}>"
end

#matches?(other) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/another_api/scope.rb', line 23

def matches?(other)
  other.group == group && (action == :all || other.action == action)
end

#qualified_nameObject



19
20
21
# File 'lib/another_api/scope.rb', line 19

def qualified_name
  "#{AnotherApi.configuration.scope_prefix}#{group}.#{action}"
end