Module: ApiResource::Scopes::ClassMethods
- Defined in:
- lib/api_resource/scopes.rb
Instance Method Summary collapse
-
#add_scopes(params, base = self) ⇒ Object
Apply scopes from params based on our resource definition.
-
#scope(scope_name, scope_definition) ⇒ Object
Called by base.rb e.g.
- #scope?(name) ⇒ Boolean
- #scope_attributes(name) ⇒ Object
-
#scopes ⇒ Object
TODO: calling these methods should force loading of the resource definition.
Instance Method Details
#add_scopes(params, base = self) ⇒ Object
Apply scopes from params based on our resource definition
76 77 78 79 80 81 |
# File 'lib/api_resource/scopes.rb', line 76 def add_scopes(params, base = self) # scopes are stored as strings but we want to allow params = params.with_indifferent_access base = self.add_static_scopes(params, base) return self.add_dynamic_scopes(params, base) end |
#scope(scope_name, scope_definition) ⇒ Object
Called by base.rb e.g. paged
e.g. => "req", "per_page" => "opt"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/api_resource/scopes.rb', line 28 def scope(scope_name, scope_definition) unless scope_definition.is_a?(Hash) raise ArgumentError, "Expecting an attributes hash given #{scope_definition.inspect}" end self.[:scopes][scope_name.to_sym] = scope_definition self.class_eval do define_singleton_method(scope_name) do |*args| arg_names = scope_definition.keys arg_types = scope_definition.values finder_opts = { scope_name => {} } arg_names.each_with_index do |arg_name, i| # If we are dealing with a scope with multiple args if arg_types[i] == :rest finder_opts[scope_name][arg_name] = args.slice(i, args.count) # Else we are only dealing with a single argument else if arg_types[i] == :req || (i < args.count) finder_opts[scope_name][arg_name] = args[i] end end end # if we have nothing at this point we should just pass 'true' if finder_opts[scope_name] == {} finder_opts[scope_name] = true end ApiResource::Conditions::ScopeCondition.new(finder_opts, self) end end end |
#scope?(name) ⇒ Boolean
13 14 15 |
# File 'lib/api_resource/scopes.rb', line 13 def scope?(name) self.[:scopes].has_key?(name.to_sym) end |
#scope_attributes(name) ⇒ Object
17 18 19 20 |
# File 'lib/api_resource/scopes.rb', line 17 def scope_attributes(name) raise "No such scope #{name}" unless self.scope?(name) self.[:scopes][name.to_sym] end |
#scopes ⇒ Object
TODO: calling these methods should force loading of the resource definition
8 9 10 11 |
# File 'lib/api_resource/scopes.rb', line 8 def scopes self.reload_resource_definition return self.[:scopes] end |