Class: RenderSync::ScopeDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/render_sync/scope_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/render_sync/scope_definition.rb', line 3

def args
  @args
end

#klassObject

Returns the value of attribute klass.



3
4
5
# File 'lib/render_sync/scope_definition.rb', line 3

def klass
  @klass
end

#lambdaObject

Returns the value of attribute lambda.



3
4
5
# File 'lib/render_sync/scope_definition.rb', line 3

def lambda
  @lambda
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/render_sync/scope_definition.rb', line 3

def name
  @name
end

#parametersObject

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