# encoding: utf-8 module Uuids # The module contains services for managing uuids. # # Any service implements the Observer pattern using the 'wisper' gem. # This means a service doesn't return results to its caller, but calls # a corresponding methods of its subscribers ("notifies subscribers"). # # @see http://en.wikipedia.org/wiki/Observer_pattern Observer pattern. # @see http://github.com/krisleech/wisper the Wisper gem. module Services # Adds the uuid to the record. # # @example # service = Uuids::Services::Add.new( # record: #, # value: "42037503-2753-0570-3257-230403275325", # ) # service.subscribe some_listener # service.run class Add < Hexx::Service # @!scope class # @!method new(params) # Constructs the service object. # @example (see Uuids::Services::Add) # @param [Hash] params The list of service options. # @option params [ActiveRecord::Base] :record The record for adding uuid. # @option params [String] :value The uuid to be added to the record. allow_params :record, :value # @!method subscribe(listener, options = {}) # Subscribes a listener to receive the service's notifications. # @example (see Uuids::Services::Add) # @param [Object] listener Any object can be subscribed as a listener. # It is expected (but not required) the listener to respond to # notifications, published by the {#run}. # @param [Hash] options The subscription options. # @option options [Symbol] :prefix (nil) The prefix to be added # to notifications to provide valid listener method names to call. # Runs the service and publishes its results. # @example (see Uuids::Services::Add) # @change Creates the uuid assigned to the record. # @publish created(uuid,messages) when the uuid has been created. # @publish error(messages) when the uuid cannot be created. def run escape { create_uuid } rescue => err publish :error, err.messages else publish :created, uuid, messages end private attr_reader :uuid def create_uuid @uuid = Models::Uuid.create! record: record, value: value add_message "success", :created, uuid: uuid, record: name end def name t :record, id: record.id end end end end