Class: Poncho::Params::ResourceParam

Inherits:
Poncho::Param show all
Defined in:
lib/poncho/params/resource.rb

Instance Attribute Summary

Attributes inherited from Poncho::Param

#name, #options

Instance Method Summary collapse

Methods inherited from Poncho::Param

type, #type

Constructor Details

#initialize(name, options = {}) ⇒ ResourceParam

Returns a new instance of ResourceParam.



4
5
6
7
8
9
10
# File 'lib/poncho/params/resource.rb', line 4

def initialize(name, options = {})
  super

  unless options[:resource]
    throw ArgumentError, ':resource required'
  end
end

Instance Method Details

#convert(value) ⇒ Object



22
23
24
# File 'lib/poncho/params/resource.rb', line 22

def convert(value)
  options[:resource].new(value)
end

#validate_each(record, attribute, value) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/poncho/params/resource.rb', line 12

def validate_each(record, attribute, value)
  resource = convert(value)

  unless resource.valid?
    resource.errors.to_hash.each do |attr, messages|
      record.messages[:"#{attribute}[#{attr}]"] |= messages
   end
  end
end