Sha256: 806e970da92579a7de3c06115cc84f0cf9f00508542f8e5e7f4ee6f51a15d472

Contents?: true

Size: 959 Bytes

Versions: 3

Compression:

Stored size: 959 Bytes

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

        private

        def params
          @params ||= []
        end

        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| send "#{ key }=", value }
      end

      private

      attr_reader :params

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hexx-2.0.2 lib/hexx/service/parameters.rb
hexx-2.0.1 lib/hexx/service/parameters.rb
hexx-2.0.0 lib/hexx/service/parameters.rb