Skip to content

Commit

Permalink
Extract common interfrace of Params and Contract
Browse files Browse the repository at this point in the history
  • Loading branch information
krzykamil committed Aug 12, 2024
1 parent 56081d2 commit 9a264e1
Show file tree
Hide file tree
Showing 10 changed files with 680 additions and 329 deletions.
68 changes: 2 additions & 66 deletions lib/hanami/action/base_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Action
# @api private
# @since 0.7.0
class BaseParams
include Hanami::Action::RequestParams::Base
# @attr_reader env [Hash] the Rack env
#
# @since 0.7.0
Expand All @@ -39,23 +40,11 @@ class BaseParams
# @api private
def initialize(env)
@env = env
@raw = _extract_params
@raw = Hanami::Action::ParamsExtraction.new(env).call
@params = Utils::Hash.deep_symbolize(@raw)
freeze
end

# Returns the value for the given params key.
#
# @param key [Symbol] the key
#
# @return [Object,nil] the associated value, if found
#
# @since 0.7.0
# @api public
def [](key)
@params[key]
end

# Returns an value associated with the given params key.
#
# You can access nested attributes by listing all the keys in the path. This uses the same key
Expand Down Expand Up @@ -86,17 +75,6 @@ def [](key)
# end
# end
#
# @since 0.7.0
# @api public
def get(*keys)
@params.dig(*keys)
end

# This is for compatibility with Hanami::Helpers::FormHelper::Values
#
# @api private
# @since 0.8.0
alias_method :dig, :get

# Returns true at all times, providing a common interface with {Params}.
#
Expand All @@ -110,50 +88,8 @@ def valid?
true
end

# Returns a hash of the parsed request params.
#
# @return [Hash]
#
# @since 0.7.0
# @api public
def to_h
@params
end
alias_method :to_hash, :to_h

# Iterates over the params.
#
# Calls the given block with each param key-value pair; returns the full hash of params.
#
# @yieldparam key [Symbol]
# @yieldparam value [Object]
#
# @return [to_h]
#
# @since 0.7.1
# @api public
def each(&blk)
to_h.each(&blk)
end

private

# @since 0.7.0
# @api private
def _extract_params
result = {}

if env.key?(Action::RACK_INPUT)
result.merge! ::Rack::Request.new(env).params
result.merge! _router_params
else
result.merge! _router_params(env)
env[Action::REQUEST_METHOD] ||= Action::DEFAULT_REQUEST_METHOD
end

result
end

# @since 0.7.0
# @api private
def _router_params(fallback = {})
Expand Down
67 changes: 7 additions & 60 deletions lib/hanami/action/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Action
#
# @since 2.2.0
class Contract
include Hanami::Action::RequestParams::ActionValidations
include Hanami::Action::RequestParams::Base
# A wrapper for the result of a contract validation
# @since 2.2.0
# @api private
Expand All @@ -22,19 +24,17 @@ def to_h
__getobj__.to_h
end

# This method is called messages not errors to be consistent with the Hanami::Validations::Result
#
# @return [Hash] the error messages
#
# @since 2.0.0
# @api private
def errors
def messages
__getobj__.errors.to_h
end
end

# @attr_reader env [Hash] the Rack env
#
# @since 2.2.0
# @api private
attr_reader :env

# Define a contract for the given action
#
# @param blk [Proc] the block to define the contract, including [Params] as a contract schema and connected rules
Expand Down Expand Up @@ -68,59 +68,6 @@ class << self
attr_reader :_validator
end

# Initialize the contract and freeze it.
#
# @param env [Hash] a Rack env or an hash of params.
#
# @return [Hash]
#
# @since 2.2.0
# @api public
def initialize(env)
@env = env
@input = Hanami::Action::ParamsExtraction.new(env).call
validation = validate
@params = validation.to_h
@errors = Hanami::Action::Params::Errors.new(validation.errors)
freeze
end

attr_reader :errors

# Returns true if no validation errors are found,
# false otherwise.
#
# @return [TrueClass, FalseClass]
#
# @since 2.2.0
#
def valid?
errors.empty?
end

# Serialize validated params to Hash
#
# @return [::Hash]
#
# @since 2.2.0
def to_h
@params
end

attr_reader :result

# Returns the value for the given params key.
#
# @param key [Symbol] the key
#
# @return [Object,nil] the associated value, if found
#
# @since 2.2.0
# @api public
def [](key)
@params[key]
end

private

# @since 2.2.0
Expand Down
Loading

0 comments on commit 9a264e1

Please sign in to comment.