Class: HaveAPI::ModelAdapters::ActiveRecord::ValidatorTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/model_adapters/active_record.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ ValidatorTranslator

Returns a new instance of ValidatorTranslator.



415
416
417
# File 'lib/haveapi/model_adapters/active_record.rb', line 415

def initialize(params)
  @params = params
end

Class Attribute Details

.handlersObject (readonly)

Returns the value of attribute handlers.



336
337
338
# File 'lib/haveapi/model_adapters/active_record.rb', line 336

def handlers
  @handlers
end

Class Method Details

.handle(validator, &block) ⇒ Object



338
339
340
341
# File 'lib/haveapi/model_adapters/active_record.rb', line 338

def handle(validator, &block)
  @handlers ||= {}
  @handlers[validator] = block
end

Instance Method Details

#translate(v) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
# File 'lib/haveapi/model_adapters/active_record.rb', line 434

def translate(v)
  self.class.handlers.each do |klass, translator|
    next unless v.is_a?(klass)

    v.attributes.each do |attr|
      @attr = attr
      instance_exec(v, &translator)
    end
    break
  end
end

#validator(key, opts) ⇒ Object



430
431
432
# File 'lib/haveapi/model_adapters/active_record.rb', line 430

def validator(key, opts)
  validator_for(@attr, key, opts)
end

#validator_for(param, key, opts) ⇒ Object



419
420
421
422
423
424
425
426
427
428
# File 'lib/haveapi/model_adapters/active_record.rb', line 419

def validator_for(param, key, opts)
  @params.each do |p|
    next unless p.is_a?(::HaveAPI::Parameters::Typed)

    if p.db_name == param
      p.add_validator(key, opts) if p.load_validators?
      break
    end
  end
end