Skip to content

Commit

Permalink
Fix coding style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Dec 26, 2016
1 parent 8fa71a5 commit ebe2f7a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
27 changes: 16 additions & 11 deletions lib/pragma/operation/authorization.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
# frozen_string_literal: true
module Pragma
module Operation
# Provides integration with {https://github.com/pragmarb/pragma-policy Pragma::Policy}.
#
# @author Alessandro Desantis
module Authorization
def self.included(base)
base.extend ClassMethods
base.include InstanceMethods
end

module ClassMethods
# Sets the contract to use for validating this operation.
module ClassMethods # :nodoc:
# Sets the policy to use for authorizing this operation.
#
# @param klass [Class] a subclass of +Pragma::Contract::Base+
def contract(klass)
@contract = klass
# @param klass [Class] a subclass of +Pragma::Policy::Base+
def policy(klass)
@policy = klass
end

# Builds the contract for the given resource, using the previous defined contract class.
# Builds the policy for the given user and resource, using the previous defined policy
# class.
#
# @param user [Object]
# @param resource [Object]
#
# @return [Pragma::Contract::Base]
# @return [Pragma::Policy::Base]
#
# @see #contract
def build_contract(resource)
@contract.new(resource)
# @see #policy
def build_policy(user:, resource:)
@policy.new(user: user, resource: resource)
end
end

module InstanceMethods
module InstanceMethods # :nodoc:
# Builds the policy for the current user and the given resource, using the previously
# defined policy class.
#
Expand Down
31 changes: 18 additions & 13 deletions lib/pragma/operation/validation.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
# frozen_string_literal: true
module Pragma
module Operation
# Provides integration with {https://github.com/pragmarb/pragma-contract Pragma::Contract}.
#
# @author Alessandro Desantis
module Validation
def self.included(base)
base.extend ClassMethods
base.include InstanceMethods
end

module ClassMethods
# Sets the policy to use for authorizing this operation.
module ClassMethods # :nodoc:
# Sets the contract to use for validating this operation.
#
# @param klass [Class] a subclass of +Pragma::Policy::Base+
def policy(klass)
@policy = klass
# @param klass [Class] a subclass of +Pragma::Contract::Base+
def contract(klass)
@contract = klass
end

# Builds the policy for the given user and resource, using the previous defined policy
# class.
# Builds the contract for the given resource, using the previous defined contract class.
#
# @param user [Object]
# @param resource [Object]
#
# @return [Pragma::Policy::Base]
# @return [Pragma::Contract::Base]
#
# @see #policy
def build_policy(user:, resource:)
@policy.new(user: user, resource: resource)
# @see #contract
def build_contract(resource)
@contract.new(resource)
end
end

module InstanceMethods
module InstanceMethods # :nodoc:
# Builds the contract for the given resource, using the previously defined contract class.
#
# This is just an instance-level alias of {.build_contract}. You should use this from inside
Expand All @@ -51,11 +52,13 @@ def build_contract(resource)
#
# @return [Boolean] whether the operation is valid
def validate(validatable)
# rubocop:disable Metrics/LineLength
contract = if defined?(Pragma::Contract::Base) && validatable.is_a?(Pragma::Contract::Base)
validatable
else
build_contract(validatable)
end
# rubocop:enable Metrics/LineLength

contract.validate(params)
end
Expand All @@ -65,11 +68,13 @@ def validate(validatable)
#
# @param validatable [Object|Pragma::Contract::Base] contract or resource
def validate!(validatable)
# rubocop:disable Metrics/LineLength
contract = if defined?(Pragma::Contract::Base) && validatable.is_a?(Pragma::Contract::Base)
validatable
else
build_contract(validatable)
end
# rubocop:enable Metrics/LineLength

return if validate(contract)

Expand Down

0 comments on commit ebe2f7a

Please sign in to comment.