Module: BankExchangeApi::Param

Included in:
Request::Base
Defined in:
lib/bank_exchange_api/param.rb

Instance Method Summary collapse

Instance Method Details

#param(*names, klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bank_exchange_api/param.rb', line 3

def param(*names, klass)
  names.each do |name|
    class_eval <<-METHODS, __FILE__, __LINE__ + 1
      def #{name}=(value)
        @#{name}=value
      end

      def #{name}
        value = @#{name} if defined?(@#{name})

        @_#{name}_ ||= case #{klass}.name
          when 'Array'
            Array(value)
          when 'String'
            String(value) if value
          when 'Integer'
            Integer(value) if value
          when 'Date'
            if value
              value.is_a?(Date) ? value : Date.parse(value.to_s)
            end
          else
            raise UnsupportedParamClass, "Provide #{klass} class processing"
        end
      end

      def #{name}!
        self.#{name} || (raise MissingRequiredParam, "Param '#{name}' is required but missing")
      end
    METHODS
  end
end