Class: RenderSync::ScopeDefinition
- Inherits:
-
Object
- Object
- RenderSync::ScopeDefinition
- Defined in:
- lib/render_sync/scope_definition.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#lambda ⇒ Object
Returns the value of attribute lambda.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
Class Method Summary collapse
-
.ensure_valid_params!(klass, lambda) ⇒ Object
Checks the validity of the parameter names contained in the lambda definition.
Instance Method Summary collapse
-
#initialize(klass, name, lambda) ⇒ ScopeDefinition
constructor
A new instance of ScopeDefinition.
Constructor Details
#initialize(klass, name, lambda) ⇒ ScopeDefinition
5 6 7 8 9 10 11 12 |
# File 'lib/render_sync/scope_definition.rb', line 5 def initialize(klass, name, lambda) self.class.ensure_valid_params!(klass, lambda) @klass = klass @name = name @lambda = lambda @parameters = lambda.parameters.map { |p| p[1] } end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
3 4 5 |
# File 'lib/render_sync/scope_definition.rb', line 3 def args @args end |
#klass ⇒ Object
Returns the value of attribute klass.
3 4 5 |
# File 'lib/render_sync/scope_definition.rb', line 3 def klass @klass end |
#lambda ⇒ Object
Returns the value of attribute lambda.
3 4 5 |
# File 'lib/render_sync/scope_definition.rb', line 3 def lambda @lambda end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/render_sync/scope_definition.rb', line 3 def name @name end |
#parameters ⇒ Object
Returns the value of attribute parameters.
3 4 5 |
# File 'lib/render_sync/scope_definition.rb', line 3 def parameters @parameters end |
Class Method Details
.ensure_valid_params!(klass, lambda) ⇒ Object
Checks the validity of the parameter names contained in the lambda definition. E.g. if the lambda looks like this:
->(user) { where(user_id: user.id) }
The name of the passed argument (user) must be present as a column name or an instance method (e.g. an association) of the ActiveRecord object.
22 23 24 25 26 27 |
# File 'lib/render_sync/scope_definition.rb', line 22 def self.ensure_valid_params!(klass, lambda) unless (invalid = lambda.parameters.map { |p| p[1] } - klass.column_names.map(&:to_sym) - klass.instance_methods) == [] raise ArgumentError, "Invalid parameters #{invalid}. Parameter names of the sync_scope lambda definition may only contain ActiveRecord column names or instance methods of #{klass.name}." end true end |