Permalink
Browse files

notifiers work again :)

  • Loading branch information...
1 parent 5dbf711 commit b506676e345d69a1bf4103be06aae224ed6e56e2 @kristianmandrup committed Aug 17, 2012
View
@@ -299,6 +299,10 @@ You have not been signed in.},
end
```
+The Notifier infrastructure consists of the following classes:
+
+*
+
## Rails Generators
`$ rails g controll:setup`
@@ -33,6 +33,7 @@ def add_action_handler name
@action_handlers ||= []
@action_handlers << name.to_s.underscore.to_sym
end
+ end
protected
@@ -32,7 +32,7 @@ def redirecter options = {}, &block
def event &block
raise ArgumentError, "Must be called with a block" unless block_given?
define_method :event do
- instance_variable_get("@event") || instance_variable_set("@event", instance_eval &block)
+ instance_variable_get("@event") || instance_variable_set("@event", instance_eval(&block))
end
end
View
@@ -25,11 +25,11 @@ def commander name, options = {}
end
end
- def message_handler name, options = {}
- define_method :message_handler do
- instance_variable_get("@message_handler") || begin
+ def notifier name, options = {}
+ define_method :notifier do
+ instance_variable_get("@notifier") || begin
clazz = "Notifiers::#{name.to_s.camelize}".constantize
- instance_variable_set "@message_handler", clazz.new(self, options)
+ instance_variable_set "@notifier", clazz.new(self, options)
end
end
end
View
@@ -2,12 +2,11 @@ module Controll
module Notify
TYPES = [:notice, :error, :warning, :success]
- autoload :Base, 'controll/notify/base'
- autoload :Flash, 'controll/notify/flash'
- autoload :Typed, 'controll/notify/typed'
- autoload :Macros, 'controll/notify/macros'
- autoload :Translator, 'controll/notify/translator'
- autoload :Message, 'controll/notify/message'
+ autoload :Base, 'controll/notify/base'
+ autoload :Flash, 'controll/notify/flash'
+ autoload :Typed, 'controll/notify/typed'
+ autoload :Macros, 'controll/notify/macros'
+ autoload :Message, 'controll/notify/message'
end
end
@@ -1,14 +1,16 @@
-require 'liquid'
require 'controll/notify/flash'
module Controll
module Notify
class Base < Flash
- class NotifyMappingError < StandardError; end
+ Message = Controll::Notify::Message
+
+ attr_reader :name
def notify name, options = {}
- @options.merge! options if options.kind_of? Hash
- signal notify_msg(name)
+ @options.merge! options
+ @name = name
+ signal message_resolver.resolve
end
def self.inherited(base)
@@ -17,49 +19,35 @@ def self.inherited(base)
module ClassMethods
def signal_type
- self.name.demodulize.sub(/Handler$/, '').underscore.to_sym
+ @signal_type ||= self.name.demodulize.sub(/Handler$/, '').underscore.to_sym
+ end
+
+ def type name
+ @signal_type = name
end
end
protected
- def notify_msg name, opts = {}
- opts = options.merge(opts).stringify_keys
-
- msg = send(name) if respond_to? name
- msg ||= messages[name.to_sym] if respond_to? :messages
- msg ||= name.to_sym
-
- message = create_message msg, opts
-
- # try various approaches!
- case msg
- when Symbol
- translate message
- when String
- return replace_args(message) if msg =~ /{{.*}}/
- msg
- else
- msg_error!
- end
- rescue
+ def message_resolver
+ @message_resolver ||= Message::Resolver.new self, message
end
- def msg_error!
- raise NotifyMappingError, "Notify message could not be generated for: #{name}"
- end
+ def message
+ @message ||= Message.new text, options
+ end
- def replace_args message
- # Parses and compiles the template
- Liquid::Template.parse(message.text).render(message.options)
+ def text
+ @text ||= resolve_text(name) || key
end
- def translate message
- translator(message).translate
+ def key
+ @key ||= name.to_sym
end
- def translator message
- Controll::Notify::Translator.new self, message
+ def resolve_text name
+ return send(name) if respond_to? name
+ messages[name.to_sym] if respond_to? :messages
end
end
end
@@ -1,10 +1,19 @@
module Controll::Notify
class Message
+ autoload :Translator, 'controll/notify/message/translator'
+ autoload :Handler, 'controll/notify/message/handler'
+ autoload :Resolver, 'controll/notify/message/resolver'
+
attr_reader :text, :options
def initialize text, options = {}
- raise ArgumentError, "Message text must be a String or Symbol" unless text.kind_of?(String)
- @text, @options = [text.to_s.strip!, options]
+ raise ArgumentError, "Message text must be a String or Symbol" unless valid_text? text
+ @text = text
+ @options = options
+ end
+
+ def translate?
+ text.kind_of? Symbol
end
protected
@@ -0,0 +1,36 @@
+require 'liquid'
+
+module Controll::Notify
+ class Message
+ class Handler
+ attr_reader :message
+
+ def initialize message
+ @message = message
+ end
+
+ def handle
+ return args? ? replace_args : message.text
+ rescue StandardError
+ nil
+ end
+
+ protected
+
+ delegate :text, to: :message
+
+ def options
+ @options ||= message.options.stringify_keys
+ end
+
+ def args?
+ text =~ /{{.*}}/
+ end
+
+ def replace_args
+ # Parses and compiles the template
+ Liquid::Template.parse(text).render(options)
+ end
+ end
+ end
+end
@@ -0,0 +1,28 @@
+module Controll::Notify
+ class Message
+ class Resolver
+ Message = Controll::Notify::Message
+
+ attr_reader :caller, :message
+
+ def initialize caller, message
+ @caller = caller
+ @message = message
+ end
+
+ def resolve
+ message.translate? ? translator.translate : message_handler.handle
+ end
+
+ protected
+
+ def message_handler
+ Message::Handler.new message
+ end
+
+ def translator
+ Message::Translator.new caller, message
+ end
+ end
+ end
+end
@@ -0,0 +1,45 @@
+module Controll
+ module Notify
+ class Message
+ class Translator
+ attr_reader :caller, :key, :options
+
+ def initialize caller, message
+ @caller = caller
+ @key = message.text
+ @options = message.options.symbolize_keys
+ end
+
+ def translate
+ I18n.t i18n_key, options
+ end
+
+ protected
+
+ def i18n_key
+ [namespace_key, key].join('.')
+ end
+
+ def namespace_key
+ [namespace, type].join('.').sub /^\\./, ''
+ end
+
+ def parts
+ @parts ||= caller.class.name.split('::')
+ end
+
+ def namespace
+ (parts.first == 'Notifiers' ? parts[1..-2] : parts[0..-2]).join('.').underscore
+ end
+
+ def type
+ parts.last.sub(/#{clazz_postfix}$/, '').underscore
+ end
+
+ def clazz_postfix
+ 'Handler'
+ end
+ end
+ end
+ end
+end
@@ -1,43 +0,0 @@
-module Controll
- module Notify
- class Translator
- attr_reader :caller, :key, :options
-
- def initialize caller, message
- @caller = caller
- @key = message.text
- @options = options.symbolize_keys unless options.blank?
- end
-
- def translate
- options ? I18n.t(i18n_key) : I18n.t(i18n_key, options)
- end
-
- protected
-
- def i18n_key
- [namespace_key, key].join('.')
- end
-
- def namespace_key
- [middle, type].join('.').sub /^./, ''
- end
-
- def parts
- @parts ||= caller.class.name.split('::')
- end
-
- def middle
- parts[1..-2].join('.').underscore
- end
-
- def type
- parts.last.sub(/#{clazz_postfix}$/, '').underscore
- end
-
- def clazz_postfix
- 'Handler'
- end
- end
- end
-end
@@ -12,7 +12,7 @@ class MissingNotifyHandlerClass < StandardError; end
clazz = handler_class(type)
raise MissingNotifyHandlerClass, "#{clazz} class missing" unless clazz
var = "@#{type}"
- instance_variable_get(var) || instance_variable_set(var, clazz.new flash)
+ instance_variable_get(var) || instance_variable_set(var, clazz.new(flash))
end
end
@@ -51,12 +51,6 @@ def service_name
end
end
- describe 'notify_msg - messages map' do
- specify do
- subject.send(:notify_msg, :must_sign_in).should == sign_in_msg
- end
- end
-
context 'I18n message' do
# services:
# notice:
@@ -85,13 +79,13 @@ def service_name
end
context 'Variable substitution' do
- context 'method calls' do
- let(:auth_error_msg) { 'Error while authenticating via facebook. The service did not return valid data.' }
+ # context 'method calls' do
+ # let(:auth_error_msg) { 'Error while authenticating via facebook. The service did not return valid data.' }
- specify do
- subject.notify('auth_error!').should == auth_error_msg
- end
- end
+ # specify do
+ # subject.notify('auth_error!').should == auth_error_msg
+ # end
+ # end
context 'Liquid templates with args' do
let(:auth_error_msg) { 'Error while authenticating via www.facebook.com/login. The service returned invalid data for the user id.' }
Oops, something went wrong.

0 comments on commit b506676

Please sign in to comment.