Class: BabySqueel::Resolver
- Inherits:
-
Object
- Object
- BabySqueel::Resolver
- Defined in:
- lib/baby_squeel/resolver.rb
Instance Method Summary collapse
-
#initialize(table, strategies) ⇒ Resolver
constructor
A new instance of Resolver.
-
#resolve(name, *args, &block) ⇒ Object
Attempt to determine the intent of the method_missing.
-
#resolve!(name, *args, &block) ⇒ Object
Try to resolve the method_missing.
-
#resolves?(name) ⇒ Boolean
Used to determine if a table can respond_to? a method.
Constructor Details
#initialize(table, strategies) ⇒ Resolver
Returns a new instance of Resolver.
3 4 5 6 |
# File 'lib/baby_squeel/resolver.rb', line 3 def initialize(table, strategies) @table = table @strategies = strategies end |
Instance Method Details
#resolve(name, *args, &block) ⇒ Object
Attempt to determine the intent of the method_missing. If this method returns nil, that means one of two things:
-
The argument signature is invalid.
-
The name of the method called is not valid (ex. invalid column name)
13 14 15 16 17 18 19 |
# File 'lib/baby_squeel/resolver.rb', line 13 def resolve(name, *args, &block) strategy = @strategies.find do |strategy| valid?(strategy, name, *args, &block) end build(strategy, name, *args, &block) end |
#resolve!(name, *args, &block) ⇒ Object
Try to resolve the method_missing. If we fail to resolve, there are two outcomes:
If any of the configured strategies accept argument signature provided, raise an error. This means we failed to resolve the name. (ex. invalid column name)
Otherwise, a nil return valid indicates that argument signature was not valid for any of the configured strategies. This case should be treated as a NoMethodError.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/baby_squeel/resolver.rb', line 31 def resolve!(name, *args, &block) if resolution = resolve(name, *args, &block) return resolution end if compatible_arguments?(*args, &block) raise NotFoundError.new(@table._scope.model_name, name, @strategies) end return nil end |
#resolves?(name) ⇒ Boolean
Used to determine if a table can respond_to? a method.
44 45 46 47 48 |
# File 'lib/baby_squeel/resolver.rb', line 44 def resolves?(name) @strategies.any? do |strategy| valid_name? strategy, name end end |