Class: Rester::Service::Resource::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/rester/service/resource/params.rb

Constant Summary collapse

BASIC_TYPES =
[String, Symbol, Float, Integer].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ Params

Returns a new instance of Params.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rester/service/resource/params.rb', line 8

def initialize(opts={}, &block)
  @options = opts.dup.freeze
  @_required_fields = []
  @_defaults = {}
  @_all_fields = []

  # Default "validator" is to just treat the param as a string.
  @_validators = Hash.new([String, {}])

  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object (private)



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rester/service/resource/params.rb', line 108

def method_missing(meth, *args)
  meth_str = meth.to_s

  if meth.to_s.match(/\A[A-Z][A-Za-z]+\z/)
    name = args.shift
    opts = args.shift || {}
    _add_validator(name, self.class.const_get(meth), opts)
  else
    super
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/rester/service/resource/params.rb', line 6

def options
  @options
end

Instance Method Details

#Boolean(name, opts = {}) ⇒ Object

Need to have special handling for Boolean since Ruby doesn’t have a Boolean type, instead it has TrueClass and FalseClass…



84
85
86
# File 'lib/rester/service/resource/params.rb', line 84

def Boolean(name, opts={})
  _add_validator(name, :boolean, opts)
end

#freezeObject



27
28
29
30
31
32
33
# File 'lib/rester/service/resource/params.rb', line 27

def freeze
  @_validators.freeze
  @_required_fields.freeze
  @_defaults.freeze
  @_all_fields.freeze
  super
end

#strict?Boolean

Whether or not validation will be done strictly (i.e., only specified params will be allowed).

Returns:



23
24
25
# File 'lib/rester/service/resource/params.rb', line 23

def strict?
  !!options[:strict]
end

#typeObject

The basic data types all have helper methods named after them in Kernel. This allows you to do things like String(1234) to get ‘1234’. It’s the same as doing 1234.to_s.

Since methods already exist globally for these types, we need to override them so we can capture their calls. If this weren’t the case, then we’d be catch them in ‘method_missing`.



75
76
77
78
79
# File 'lib/rester/service/resource/params.rb', line 75

BASIC_TYPES.each do |type|
  define_method(type.to_s) { |name, opts={}|
    _add_validator(name, type, opts)
  }
end

#use(params) ⇒ Object



62
63
64
65
# File 'lib/rester/service/resource/params.rb', line 62

def use(params)
  _merge_params(params)
  nil
end

#validate(params) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rester/service/resource/params.rb', line 35

def validate(params)
  param_keys = params.keys.map(&:to_sym)
  default_keys = @_defaults.keys

  unless (missing = @_required_fields - param_keys - default_keys).empty?
    _error!("missing params: #{missing.join(', ')}")
  end

  if strict? && !(unexpected = param_keys - @_all_fields).empty?
    _error!("unexpected params: #{unexpected.join(', ')}")
  end

  validated_params = params.map do |key, value|
    [key.to_sym, validate!(key.to_sym, value)]
  end.to_h

  @_defaults.merge(validated_params)
end

#validate!(key, value) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rester/service/resource/params.rb', line 54

def validate!(key, value)
  klass = @_validators[key].first

  _parse_with_class(klass, value).tap do |obj|
    _validate_obj(key, obj)
  end
end