class DDC::ServiceBuilder

Attributes

ar_model[RW]
finder[RW]
model_type[RW]

Public Class Methods

build(model_type) click to toggle source
# File lib/ddc/service_builder.rb, line 3
def self.build(model_type)
  Class.new do
    include DDC::ResponseBuilder
    class << self
      attr_accessor :model_type, :ar_model, :finder
    end

    @model_type = model_type
    ar_class_name = model_type.to_s.camelize
    @ar_model = Object.const_get(ar_class_name)

    @finder = nil
    begin
      @finder = Object.const_get("#{ar_class_name}Finder")
    rescue NameError
      # we use the AR Model as a fallback
    end

    def find(context)
      id = context[:id]
      me = custom_finder ? custom_finder.find(context) : 
                           ar_model.where(id: id)
      if me.present?
        ok(me)
      else
        not_found
      end
    end

    def find_all(context={})
      mes = custom_finder ? custom_finder.find_all(context) : 
                            ar_model.all
      ok(mes)
    end

    def update(context)
      id, updates = context.values_at :id, self.class.model_type
      me = self.class.ar_model.where id: id

      if me.present?
        success = me.update_attributes translated_updates
        if success
          ok(me)
        else
          not_valid(me)
        end
      else
        not_found
      end
    end

    def create(context)
      attributes = context.values_at self.class.model_type
      me = self.class.ar_model.create attributes
      if me.persisted?
        created(me)
      else
        not_valid(me)
      end
    end

    def delete(context)
      id = context[:id]
      me = custom_finder ? custom_finder.find(context) : 
                           ar_model.where(id: id)
      if me.present?
        me.destroy
        deleted
      else
        not_found
      end
    end

    private
    def custom_finder
      self.class.finder
    end

    def ar_model
      self.class.ar_model
    end

  end
end

Public Instance Methods

ar_model() click to toggle source
# File lib/ddc/service_builder.rb, line 81
def ar_model
  self.class.ar_model
end
create(context) click to toggle source
# File lib/ddc/service_builder.rb, line 54
def create(context)
  attributes = context.values_at self.class.model_type
  me = self.class.ar_model.create attributes
  if me.persisted?
    created(me)
  else
    not_valid(me)
  end
end
custom_finder() click to toggle source
# File lib/ddc/service_builder.rb, line 77
def custom_finder
  self.class.finder
end
delete(context) click to toggle source
# File lib/ddc/service_builder.rb, line 64
def delete(context)
  id = context[:id]
  me = custom_finder ? custom_finder.find(context) : 
                       ar_model.where(id: id)
  if me.present?
    me.destroy
    deleted
  else
    not_found
  end
end
find(context) click to toggle source
# File lib/ddc/service_builder.rb, line 21
def find(context)
  id = context[:id]
  me = custom_finder ? custom_finder.find(context) : 
                       ar_model.where(id: id)
  if me.present?
    ok(me)
  else
    not_found
  end
end
find_all(context={}) click to toggle source
# File lib/ddc/service_builder.rb, line 32
def find_all(context={})
  mes = custom_finder ? custom_finder.find_all(context) : 
                        ar_model.all
  ok(mes)
end
update(context) click to toggle source
# File lib/ddc/service_builder.rb, line 38
def update(context)
  id, updates = context.values_at :id, self.class.model_type
  me = self.class.ar_model.where id: id

  if me.present?
    success = me.update_attributes translated_updates
    if success
      ok(me)
    else
      not_valid(me)
    end
  else
    not_found
  end
end