Class: Ronflex::Configuration
- Inherits:
-
Object
- Object
- Ronflex::Configuration
- 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.
["/health_check", "/favicon.ico"]
- DEFAULT_PROVIDER =
Default provider. by default, it is a lambda which returns ‘nil`.
-> (env) { nil }
Instance Attribute Summary collapse
-
#enable ⇒ Boolean
Attribute for desable or enable th feature.
-
#excluded_path ⇒ Array<String>
List of paths excluded from rule evaluation.
-
#maintenance_page ⇒ String
Path to a custom maintenance page (either an HTML or ERB file).
-
#provider ⇒ Proc
Custom provider to retrieve a model based on the environment.
-
#rules ⇒ Array<Rule>
List of defined access rules.
Instance Method Summary collapse
-
#add_rule(type) {|model, request| ... } ⇒ void
Adds an access rule for a specific type.
-
#allowed?(model, request) ⇒ Boolean
Checks if a model is allowed to access a given query.
-
#initialize ⇒ Configuration
constructor
Initializes a new configuration with default values.
-
#model_present?(model) ⇒ Boolean
Checks if a model is valid (present).
Constructor Details
#initialize ⇒ Configuration
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
#enable ⇒ Boolean
Attribute for desable or enable th feature
50 51 52 |
# File 'lib/ronflex/configuration.rb', line 50 def enable @enable end |
#excluded_path ⇒ Array<String>
List of paths excluded from rule evaluation.
55 56 57 |
# File 'lib/ronflex/configuration.rb', line 55 def excluded_path @excluded_path end |
#maintenance_page ⇒ String
Path to a custom maintenance page (either an HTML or ERB file).
70 71 72 |
# File 'lib/ronflex/configuration.rb', line 70 def maintenance_page @maintenance_page end |
#provider ⇒ Proc
Custom provider to retrieve a model based on the environment.
60 61 62 |
# File 'lib/ronflex/configuration.rb', line 60 def provider @provider end |
#rules ⇒ Array<Rule>
List of defined access rules.
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
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
113 114 115 |
# File 'lib/ronflex/configuration.rb', line 113 def allowed?(model, request) rules.empty? || rules.any? { |rule| rule.matches?(model, request) } end |
#model_present?(model) ⇒ Boolean
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.
125 126 127 |
# File 'lib/ronflex/configuration.rb', line 125 def model_present?(model) !model.nil? && !(model.respond_to?(:empty?) && model.empty?) end |