Class: Shamu::Entities::ListScope

Inherits:
Object
  • Object
show all
Includes:
Attributes, Attributes::Assignment, Attributes::Validation
Defined in:
lib/shamu/entities/list_scope.rb,
lib/shamu/entities/list_scope/dates.rb,
lib/shamu/entities/list_scope/paging.rb,
lib/shamu/entities/list_scope/sorting.rb,
lib/shamu/entities/list_scope/scoped_paging.rb,
lib/shamu/entities/list_scope/window_paging.rb

Overview

The desired scope of entities offered Services::Service to prepare a list of entities.

Standard scopes

Examples:

class UsersListScope < Shamu::Entities::ListScope

  # Include standard paging options (page, per_page) from ListScopes::Paging
  include Shamu::Entities::ListScope::Paging

  # Allow client to request that users be limited to those in one of the
  # given roles.
  attribute :roles, array: true, coerce: :to_s
end

Direct Known Subclasses

Auditing::ListScope, Features::ListScope

Defined Under Namespace

Modules: Dates, Paging, ScopedPaging, Sorting, WindowPaging

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::Validation

attribute, #valid?, #validated?

Methods included from Attributes::Assignment

#[]=, #assigned?, #assigned_attributes, attribute, #unassigned_attributes

Methods included from Attributes

#[], #as_json, #assign_attributes, association, associations, attribute, #attribute?, attributes, #initialize, #pretty_print, #set?, #slice, #to_attributes, #to_json

Class Method Details

.coerce(params) ⇒ ListScope

Coerces a hash or params object to a proper ListScope object.

Parameters:

  • params (Object)

    to be coerced.

Returns:



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shamu/entities/list_scope.rb', line 56

def coerce( params )
  if params.is_a?( self )
    params
  elsif params.respond_to?( :to_h ) || params.respond_to?( :to_attributes )
    new( params )
  elsif params.nil?
    new
  else
    raise ArgumentError
  end
end

.coerce!(params) ⇒ ListScope

Coerces the given params object and raises an ArgumentError if any of the parameters are invalid.

Parameters:

  • params (Object)

    to be coerced.

Returns:

Raises:

  • (ArgumentError)


72
73
74
75
76
# File 'lib/shamu/entities/list_scope.rb', line 72

def coerce!( params )
  coerced = coerce( params )
  raise ArgumentError unless coerced.valid?
  coerced
end

.for(entity_class) ⇒ ListScope

Finds the natural Shamu::Entities::ListScope class for the given entity class.

Users::UserEntity -> Users::UserListScope or Users::ListScope

Parameters:

  • entity_class (Class)

    the Entity class to find a scope for.

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/shamu/entities/list_scope.rb', line 85

def for( entity_class )
  base_name = entity_class.name || "Entity"
  name = base_name.sub /(Entity)?$/, "ListScope"
  begin
    return name.constantize

    # Rescuing instead of using const_defined? so that active record can
    # do it's auto-loading magic.
  rescue NameError # rubocop:disable Lint/HandleExceptions
  end

  name = base_name.sub /::[A-Za-z0-9]+(Entity)?$/, "::ListScope"

  begin
    return name.constantize
  rescue NameError # rubocop:disable Lint/HandleExceptions
  end

  self
end

Instance Method Details

#except(*param_names) ⇒ ListScope

Clone the params but exclude the given parameters.

Parameters:

  • param_names (Array<Symbol>)

    to exclude.

Returns:



39
40
41
# File 'lib/shamu/entities/list_scope.rb', line 39

def except( *param_names )
  self.class.new( to_attributes( except: param_names ) )
end

#paramsHash

Returns the hash of attributes that can be used to generate a url.

Returns:

  • (Hash)

    the hash of attributes that can be used to generate a url.



44
45
46
47
48
49
50
# File 'lib/shamu/entities/list_scope.rb', line 44

def params
  params = to_attributes
  params.each do |key, value|
    params[key] = value.params if value.respond_to?( :params )
  end
  params
end