Sha256: 92900fc2edac6a5537d6cc5c93b451839fce81cee66c6ebe8c639a33ec24e78f
Contents?: true
Size: 1.9 KB
Versions: 5
Compression:
Stored size: 1.9 KB
Contents
module Hexx class Service # @api hide # Contains methods to declare parameters and set their values. module Parameters extend ActiveSupport::Concern # Methods to declare and allow services params. module ClassMethods # @!attribute [r] params # The list of allowed instance parameters. The parameters are added # to the list by the {.allow_params} method. # # @example # class Service # include Parameters # allow_params :name # end # # Service.params # => "name" # # @return [Array<String>] The list of allowed instance parameters. def params @params ||= [] end private # Sets a list of allowed parameters for the class constructor and # defines the corresponding instance attributes. # # @example (see Hexx::Service::Parameters.params) # @param [Array<Symbol, String>] keys The list of allowed parameters. def allow_params(*keys) @params = keys.map(&:to_s) attr_accessor(*params) end end # @!method new(params) # Constructs a service object with a hash of parameters. # # @example # Service.new name: "name" # # @param [Hash] params ({}) The parameters of the service object. def initialize(params = {}) extract_params_from params @params.each { |key, value| public_send "#{ key }=", value } end # @!attribute [r] params # The parameters of the service objects. The parameters are set on # the object initialization, then are stringified and whitelisted. # @return [Hash] the service object parameters. attr_reader :params private def extract_params_from(hash) @params = hash.stringify_keys.slice(*(self.class.params)) end end end end
Version data entries
5 entries across 5 versions & 1 rubygems