Module: Acoustid::API::Request::ParamAttributes

Included in:
Base
Defined in:
lib/acoustid/api/request/param_attributes.rb

Instance Method Summary collapse

Instance Method Details

#param(name, options = {}) ⇒ Object



48
49
50
51
# File 'lib/acoustid/api/request/param_attributes.rb', line 48

def param(name, options={})
  param_reader(name, options)
  param_writer(name, options)
end

#param_reader(param, options = {}) ⇒ Object

Raises:

  • (TypeError)


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/acoustid/api/request/param_attributes.rb', line 7

def param_reader(param, options={})
  raise TypeError, 'param must respond to :to_sym' unless param.respond_to?(:to_sym)
  param = param.to_sym
  
  raise TypeError, 'options must respond to :to_hash or :to_h' unless options.respond_to?(:to_hash) || options.respond_to?(:to_h)
  options = options.to_hash rescue options.to_h
  
  name = options[:name].to_s.strip
  name = name.empty? ? param.to_s : name
  
  define_method(param) { (@params ||= {})[name] }
end

#param_writer(param, options = {}) ⇒ Object

Raises:

  • (TypeError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/acoustid/api/request/param_attributes.rb', line 20

def param_writer(param, options={})
  raise TypeError, 'param must respond to :to_sym' unless param.respond_to?(:to_sym)
  param = param.to_sym
  
  raise TypeError, 'options must respond to :to_hash or :to_h' unless options.respond_to?(:to_hash) || options.respond_to?(:to_h)
  options = options.to_hash rescue options.to_h
  
  name = options[:name].to_s.strip
  name = name.empty? ? param.to_s : name
  
  required_params << param if options[:required]
  
  raise TypeError, 'validate options must respond to :call or :to_sym' unless options[:validate].nil? || options[:validate].respond_to?(:call) || options[:validate].respond_to?(:to_sym)
  validate = options[:validate]
  validate = method(validate) if validate.respond_to?(:to_sym)
  
  raise TypeError, 'serialize options must respond to :call or :to_sym' unless options[:serialize].nil? || options[:serialize].respond_to?(:call) || options[:serialize].respond_to?(:to_sym)
  serialize = options[:serialize]
  serialize = method(serialize) if serialize.respond_to?(:to_sym)
  
  define_method("#{param}=") do |value|
    validate.call(value) unless validate.nil?
    value = serialize.call(value) unless serialize.nil?
    
    (@params ||= {})[name] = value
  end
end

#required_paramsObject



53
54
55
# File 'lib/acoustid/api/request/param_attributes.rb', line 53

def required_params
  @required_params ||= []
end