Class: Ronflex::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/ronflex/configuration.rb

Constant Summary collapse

DEFAULT_EXCLUDED_PATHS =

List of paths excluded by default. These paths are ignored by the defined rules.

Returns:

  • (Array<String>)
["/health_check"]
DEFAULT_PROVIDER =

Default provider. by default, it is a lambda which returns ‘nil`.

Returns:

  • (Proc)
-> (env) { nil }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initializes a new configuration with default values.



73
74
75
76
77
78
79
# File 'lib/ronflex/configuration.rb', line 73

def initialize
  @excluded_path = DEFAULT_EXCLUDED_PATHS.dup
  @provider = DEFAULT_PROVIDER
  @enable = false
  @maintenance_page = nil
  @rules = []
end

Instance Attribute Details

#enableBoolean

Attribute for desable or enable th feature

Returns:

  • (Boolean)


50
51
52
# File 'lib/ronflex/configuration.rb', line 50

def enable
  @enable
end

#excluded_pathArray<String>

List of paths excluded from rule evaluation.

Returns:

  • (Array<String>)


55
56
57
# File 'lib/ronflex/configuration.rb', line 55

def excluded_path
  @excluded_path
end

#maintenance_pageString

Path to a custom maintenance page (either an HTML or ERB file).

Returns:

  • (String)


70
71
72
# File 'lib/ronflex/configuration.rb', line 70

def maintenance_page
  @maintenance_page
end

#providerProc

Custom provider to retrieve a model based on the environment.

Returns:

  • (Proc)


60
61
62
# File 'lib/ronflex/configuration.rb', line 60

def provider
  @provider
end

#rulesArray<Rule>

List of defined access rules.

Returns:



65
66
67
# File 'lib/ronflex/configuration.rb', line 65

def rules
  @rules
end

Instance Method Details

#add_rule(type) {|model, request| ... } ⇒ void

This method returns an undefined value.

Adds an access rule for a specific type.

A rule is a block of code that determines whether a model is allowed to access a given query.

config.add_rule(:admin) do |model, request| request.path.start_with?(“/admin”) end

Examples:

Add a rule for administrators

Parameters:

  • type (Symbol, String)

    The type or role of the model (e.g. ‘:admin`, `:user`).

Yields:

  • (model, request)

    The rule block, which takes a model and a request as parameters.

Yield Parameters:

  • model (Object)

    The evaluated model.

  • request (Object)

    The evaluated request.

Raises:

  • (RonflexArgumentError)


95
96
97
98
99
# File 'lib/ronflex/configuration.rb', line 95

def add_rule(type, &block)
  raise RonflexArgumentError, "Rule type must be provided" if type.nil?
  raise RonflexArgumentError, "Block must be provided for the rule" unless block_given?
  @rules << Rule.new(type, &block)
end

#allowed?(model, request) ⇒ Boolean

Checks if a model is allowed to access a given query.

This method iterates through the defined rules and applies those matching the pattern.

model = :admin request = OpenStruct.new(path: “/admin/dashboard”) config.allowed?(model, request) # => true or false

Examples:

Check an authorization

Parameters:

  • model (Object)

    The model to check (e.g. a user or a symbolic role).

  • request (Object)

    The request to check, which should respond to methods like ‘path`.

Returns:

  • (Boolean)

    ‘true` if at least one rule authorizes access, `false` otherwise.



113
114
115
# File 'lib/ronflex/configuration.rb', line 113

def allowed?(model, request)
  rules.any? { |rule| rule.matches?(model, request) }
end

#model_present?(model) ⇒ Boolean

Note:

This method uses the ‘present?` method, which is typically available in Rails.

Checks if a model is valid (present).

If you are not using Rails, you may need to override this method.

Parameters:

  • model (Object)

    The model to check.

Returns:

  • (Boolean)

    ‘true` if the model is valid, `false` otherwise.



125
126
127
# File 'lib/ronflex/configuration.rb', line 125

def model_present?(model)
  !model.nil? && !(model.respond_to?(:empty?) && model.empty?)
end