Sha256: 667d998cb6b2db7f079059cd67acb8bc3ed17795640671dbbfa3f3b95eb2912a

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'active_support/core_ext/class/attribute'
require 'pio/ruby_dumper'

module Pio
  # Base message class
  class Message
    include RubyDumper

    class_attribute :options

    def self.option(name, value: nil, default: nil)
      self.options ||= {}
      if value && !value.is_a?(Symbol)
        class_eval { class_attribute name }
        class_eval { __send__("#{name}=", value) }
      end
      self.options.merge! name => { value: value, default: default }
    end

    private

    # rubocop:disable MethodLength
    # rubocop:disable AbcSize
    # rubocop:disable PerceivedComplexity
    # rubocop:disable CyclomaticComplexity
    def parse_options(user_options)
      options = {}
      self.class.options.each_pair do |key, attrs|
        if !attrs[:value] && !attrs[:default]
          begin
            options[key] = user_options.fetch(key)
          rescue
            raise "#{key} option is a mandatory"
          end
        elsif attrs[:value] && attrs[:value].is_a?(Symbol)
          options[key] = user_options.fetch(attrs[:value])
        elsif attrs[:value]
          options[key] = user_options[key] || attrs[:value]
        else
          options[key] = user_options[key] || attrs[:default]
        end
      end
      options
    end
    # rubocop:enable MethodLength
    # rubocop:enable AbcSize
    # rubocop:enable PerceivedComplexity
    # rubocop:enable CyclomaticComplexity
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pio-0.30.1 lib/pio/message.rb