Skip to content

Instantly share code, notes, and snippets.

@andrebras
Last active December 20, 2015 17:29
Show Gist options
  • Save andrebras/6168810 to your computer and use it in GitHub Desktop.
Save andrebras/6168810 to your computer and use it in GitHub Desktop.
class Automation::RulePatternFactory < Ruleby::Rulebook
include Ruleby
def patterns_from rules = []
@rules = rules
build_rules
end
protected
def build_rules
@rules.each do |r|
klass = r.entity_type.constantize
klass_ref = r.entity_type.delete( r.entity_type.tr '^:', '' ).underscore.to_sym
pattern = [klass, klass_ref, -> { m.id == r.entity_id }]
if r.conditions.nil? || r.conditions.empty?
pattern
else
r.conditions.each { |c| pattern << [klass, klass_ref, -> {m.send(c.key) == c.value}] }
pattern
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment