lib/hexx/service.rb in hexx-6.0.2 vs lib/hexx/service.rb in hexx-6.0.3

- old
+ new

@@ -22,26 +22,43 @@ class Service extend Dependable include Wisper::Publisher include ActiveModel::Validations - # @api hide - # Returns the list of allowed parameters for service objects. - # - # The parameters are added to the list by the {.allow_params} private - # helper method. - # - # @example - # class Service < Hexx::Service - # allow_params :name - # end - # - # Service.params # => "name" - # - # @return [Array<String>] The list of allowed instance parameters. - def self.params - @params ||= [] + # Class helper methods + class << self + + private + + # @api hide + # Returns the list of allowed parameters for service objects. + # + # The parameters are added to the list by the {.allow_params} private + # helper method. + # + # @example + # class Service < Hexx::Service + # allow_params :name + # end + # + # Service.params # => "name" + # + # @return [Array<String>] Whitelist of parameters. + def params + @params ||= [] + end + + # 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.flatten.map(&:to_s) + fail ArgumentError if @params == [] + params.each { |name| Helpers::Parameter.add self, name } + end end # @!scope class # @!method validates(attribute, options) # Adds a standard validation for the attribute. @@ -70,11 +87,11 @@ # @api hide # Initializes the service object. # @param (see Hexx::Service.new) # @return (see Hexx::Service.new) def initialize(params = {}) - @params = params.dup.stringify_keys.slice(*(self.class.params)) + @params = params.dup.stringify_keys.slice(*class_params) @messages = [] end # @!attribute [r] params # The list of service object parameters. @@ -109,11 +126,11 @@ # service.messages # # => [#<Hexx::Service::Message @text="ok" @type="info" >] # # @return [Array<Hexx::Service::Message>] The array of messages. - attr_reader :params, :messages + attr_reader :messages # @!scope class # @!visibility private # @!method allow_params(*params) # Whitelists {#params} and declares a parameter for corresponding keys. @@ -173,17 +190,15 @@ WithCallbacks.new(self, prefix: prefix) 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 self.allow_params(*keys) - @params = keys.map(&:to_s) - params.each { |name| Helpers::Parameter.add self, name } + attr_reader :params + + # @api hide + # @return [Array<String>] Whitelist of parameters. + def class_params + self.class.send :params end # The helper runs another service object and subscribes +self+ for the # service object's notifications. #