public
Last active

  • Download Gist
request_expressions.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
# Stripped-down version of Consent's expression language
# http://blog.jcoglan.com/2009/01/07/writing-your-own-expression-language-in-ruby/
# http://blog.jcoglan.com/2009/01/16/and-now-the-rules/
 
require 'observer'
 
module Consent
class Expression
def initialize(controller, params = {})
@controller, @params = controller.to_s, params.dup
end
def +(expression)
self.class.const_get(: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 verb=(verb)
@verb = verb.to_s
end
def method_missing(name, params = {})
@format = name.to_s if @action
@action ||= name.to_s
@params.update(params)
self
end
def inspect
source = @controller.dup
source << ".#{ @action }" if @action
source << "(#{ @params.map { |k,v| ":#{k} => #{v.inspect}" } * ', ' })" unless @params.empty?
source << "#{ @action ? '.' : '*' }#{ @format }" if @format
source = "#{ @verb }(#{ source })" if @verb
source
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
def verb=(verb)
each { |exp| exp.verb = verb }
end
def inspect
collect { |exp| exp.inspect } * ' + '
end
end
module Generator
def method_missing(name, params = {})
Expression.new(name, params)
end
end
end
class Rule
def initialize(expression, block)
@expression, @predicate = expression, block
@expression.add_observer(self)
end
 
def update(message)
@invalid = true if message == :destroyed
end
def inspect
string = @expression.inspect
string << "#invalid" if @invalid
string
end
class Expression < Consent::Expression
include Observable
attr_reader :block
def initialize(env, name, params = {})
@env = env
super(name, params)
end
def rule!(block)
return if block.nil?
@block = block
@env.rules << Rule.new(self, block)
end
def *(expression)
expression.destroy!
rule!(expression.block)
super
end
def destroy!
changed(true)
notify_observers(:destroyed)
end
def method_missing(name, params = {}, &block)
rule!(block)
super(name, params)
end
class Group < Consent::Expression::Group
attr_reader :block
def +(expression)
rule!(expression.block)
super
end
def rule!(block)
return if block.nil?
@block = block
each { |exp| exp.rule!(block) }
end
end
end
module Generator
def self.included(host)
host.module_eval do
def rules
@rules ||= []
end
end
end
 
def method_missing(name, params = {}, &block)
expression = Rule::Expression.new(self, name, params)
expression.rule!(block)
expression
end
%w(get post put head delete).each do |verb|
module_eval <<-EOS
def #{verb}(*exprs, &block)
group = exprs.inject { |grp, exp| grp + exp }
group.verb = :#{verb}
group.rule!(block)
group
end
EOS
end
 
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.