Created
January 7, 2009 00:02
-
-
Save jcoglan/44084 to your computer and use it in GitHub Desktop.
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
# Stripped-down version of Consent's expression language | |
module Consent | |
class Expression | |
def initialize(controller, params = {}) | |
@controller, @params = controller.to_s, params.dup | |
end | |
def +(expression) | |
Group.new + self + expression | |
end | |
def *(expression) | |
@format = expression.instance_eval { @controller } | |
@action = nil | |
self | |
end | |
def /(expression) | |
expression.nesting = @controller | |
expression | |
end | |
def nesting=(name) | |
@controller = "#{ name }/#{ @controller }" | |
end | |
def method_missing(name, params = {}) | |
@format = name.to_s if @action | |
@action ||= name.to_s | |
@params.update(params) | |
self | |
end | |
class Group | |
include Enumerable | |
def initialize | |
@exprs = [] | |
end | |
def each(&block) | |
@exprs.each(&block) | |
end | |
def +(expression) | |
expression.is_a?(Enumerable) ? | |
expression.each { |exp| @exprs << exp } : | |
@exprs << expression | |
self | |
end | |
end | |
module Generator | |
def method_missing(name, params = {}) | |
Expression.new(name, params) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment