Module: Appetizer::Populator::Helpers

Defined in:
lib/appetizer/populator.rb

Instance Method Summary collapse

Instance Method Details

#create(ctor, &block) ⇒ Object

Call ‘ctor.new` to create a new model object, then populate, save, and JSONify as in `update`.



42
43
44
45
46
47
# File 'lib/appetizer/populator.rb', line 42

def create ctor, &block
  obj = populate ctor.new, &block
  obj.save!

  halt 201, json(obj)
end

#populate(obj, &block) ⇒ Object

Use a populator to assign values from ‘params` to `obj`, returning it when finished. `&block` is passed a populator instance.



53
54
55
# File 'lib/appetizer/populator.rb', line 53

def populate obj, &block
  Populator.new(obj, params, &block).target
end

#update(obj, &block) ⇒ Object

Populate (see ‘populate`) an `obj` with `params` data, saving when finished. Returns JSON for `obj`.



60
61
62
63
# File 'lib/appetizer/populator.rb', line 60

def update obj, &block
  populate(obj, &block).save!
  json obj
end