Sha256: 4a6b12f84da078c2ea349489c4895afe1666265cc51ef966c895da40d819949a

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Hexx
  class Service

    # Contains methods to declare parameters and set their values.
    module Parameters
      extend ActiveSupport::Concern

      # Methods to declare and allow services params.
      module ClassMethods

        # A list of allowed params for the service objects.
        def params
          @params ||= []
        end

        private

        # Sets a list of allowed parameters for the class constructor and
        # adds corresponding attributes to instance methods.
        def allow_params(*keys)
          @params = keys.map(&:to_s)
          attr_accessor(*params)
        end
      end

      # Initializes a service object with a hash of parameters.
      #
      # @example
      #   Service.new(hash = {})
      #
      # Params:
      # +hash+:: a hash of parameters for the service.
      #
      def initialize(hash = {})
        extract_params_from hash
        params.each { |key, value| public_send "#{ key }=", value }
      end

      attr_reader :params

      private

      def extract_params_from(hash)
        @params = hash.stringify_keys.slice(*(self.class.params))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hexx-2.2.0 lib/hexx/service/parameters.rb