Class: RolePlays::Mixin::RoleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/role_plays/mixin.rb

Overview

Collects action, permitted_attributes and scope declarations of a single role block

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoleBuilder

Returns a new instance of RoleBuilder.



140
141
142
143
144
# File 'lib/role_plays/mixin.rb', line 140

def initialize
  @actions = {}
  @attributes = {}
  @scopes = {}
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



138
139
140
# File 'lib/role_plays/mixin.rb', line 138

def actions
  @actions
end

#attributesObject (readonly)

Returns the value of attribute attributes.



138
139
140
# File 'lib/role_plays/mixin.rb', line 138

def attributes
  @attributes
end

#scopesObject (readonly)

Returns the value of attribute scopes.



138
139
140
# File 'lib/role_plays/mixin.rb', line 138

def scopes
  @scopes
end

Class Method Details

.build(role, &block) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/role_plays/mixin.rb', line 130

def self.build(role, &block)
  builder = new
  builder.instance_eval(&block) if block

  Role.new(role: role.to_sym, actions: builder.actions, permitted_attributes: builder.attributes,
           scopes: builder.scopes)
end

Instance Method Details

#action(name, handler = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
# File 'lib/role_plays/mixin.rb', line 146

def action(name, handler = nil, &block)
  callable = handler || block
  raise ArgumentError, "action :#{name} requires a lambda or a block" if callable.nil?
  raise ArgumentError, "action :#{name} handler must not take required arguments" if callable.arity.positive?

  @actions[name.to_sym] = callable
end

#permitted_attributes(label = DEFAULT_ATTRIBUTES_LABEL, attributes = nil, &block) ⇒ Object

Declares the attributes a role may submit, keyed by label (:default when omitted). The attributes can be listed literally or computed by a lambda or a block:

permitted_attributes %i[title description]
permitted_attributes :list, %i[page per_page]
permitted_attributes(:create) { own_order? ? %i[title user_id] : %i[title] }

Raises:

  • (ArgumentError)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/role_plays/mixin.rb', line 160

def permitted_attributes(label = DEFAULT_ATTRIBUTES_LABEL, attributes = nil, &block)
  unless label.is_a?(Symbol)
    attributes = label
    label = DEFAULT_ATTRIBUTES_LABEL
  end

  attributes ||= block
  raise ArgumentError, "permitted_attributes :#{label} requires a list, a lambda or a block" if attributes.nil?

  if attributes.respond_to?(:call) && attributes.arity.positive?
    raise ArgumentError, "permitted_attributes :#{label} handler must not take required arguments"
  end

  @attributes[label.to_sym] = attributes.respond_to?(:call) ? attributes : -> { attributes }
end

#scope(label = DEFAULT_SCOPE_LABEL, handler = nil, &block) ⇒ Object

Declares how a role narrows a relation, keyed by a label (:default when omitted). The body reads the keyword the policy carries its relation in:

scope -> { order_relation.where(user_id: user.id) }
scope :list, -> { order_relation.where(sent: true) }
scope(:report) { own_orders.where(created_at: period) }

Raises:

  • (ArgumentError)


182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/role_plays/mixin.rb', line 182

def scope(label = DEFAULT_SCOPE_LABEL, handler = nil, &block)
  unless label.is_a?(Symbol)
    handler = label
    label = DEFAULT_SCOPE_LABEL
  end

  handler ||= block
  raise ArgumentError, "scope :#{label} requires a lambda or a block" if handler.nil?
  raise ArgumentError, "scope :#{label} handler must not take required arguments" if handler.arity.positive?

  @scopes[label.to_sym] = handler
end