-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cache helper module and ref reactions
- Loading branch information
Bartosz Kopiński
committed
Sep 20, 2013
1 parent
c3ea2fb
commit 1280161
Showing
10 changed files
with
127 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Hipbot | ||
module Cache | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
extend ClassMethods | ||
end | ||
|
||
def _cache | ||
@_cache ||= {} | ||
end | ||
|
||
module ClassMethods | ||
def attr_cache *attributes, &block | ||
attributes.each do |attr_name| | ||
define_method(attr_name) do | ||
_cache[attr_name] ||= block_given? ? instance_eval(&block) : [] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,38 @@ | ||
module Hipbot | ||
module Reactable | ||
include Cache | ||
|
||
attr_cache :reactions, :default_reactions, :options_stack | ||
attr_cache :reaction_factory do | ||
ReactionFactory.new(self) | ||
end | ||
|
||
def on *params, &block | ||
scope *params do | ||
reactions << to_reaction(params, block) | ||
reactions << to_reaction(block) | ||
end | ||
end | ||
|
||
def default *params, &block | ||
scope *params do | ||
default_reactions << to_reaction(params, block) | ||
default_reactions << to_reaction(block) | ||
end | ||
end | ||
|
||
def scope *params, &block | ||
options_stack << reaction_factory.to_reaction_options(params) | ||
options_stack << reaction_factory.get_reaction_options(params) | ||
yield | ||
options_stack.pop | ||
end | ||
|
||
def desc(text) | ||
def desc text | ||
reaction_factory.description(text) | ||
end | ||
|
||
def reactions | ||
@reactions ||= [] | ||
end | ||
|
||
def default_reactions | ||
@default_reactions ||= [] | ||
end | ||
|
||
protected | ||
|
||
def to_reaction params, block | ||
reaction_factory.build(params, block, scope_options) | ||
end | ||
|
||
def scope_options | ||
options_stack.inject { |all, h| all.merge(h) } | ||
end | ||
|
||
def options_stack | ||
@options_stack ||= [] | ||
end | ||
|
||
def reaction_factory | ||
@reaction_factory ||= ReactionFactory.new(self) | ||
def to_reaction block | ||
reaction_factory.build(options_stack, block) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters