module StairMaster class Step attr_accessor :label, :path, :conditions attr_reader :key def initialize(key, label, path, conditions) @key = key @label = label @path = path @conditions = build_conditions conditions end ## Methods -------------------------------------------- def skip?(context) rules = @conditions[:skippable] ( rules.empty? ? [false] : test_rules(rules, context) ).all? end def test_rules(rules, context) results = [] rules.each{ |condition| results << condition.test(context) } results end def url_for(resources=[], options={}) begin ## TODO: There is probably a better way to handle this. resources = resources.nil? ? [] : resources Rails.application.routes.url_helpers.send @path.to_sym, (resources.empty? ? nil : resources), options rescue Exception => e raise RuntimeError, "Could not find the route '#{ @path }' in your application. #{ e.message }" end end def to_s label end private # --------------------------------------------- def build_conditions(conditions) conditions = conditions.empty? ? {} : conditions.first skip_rules = [] conditions.each do |k,v| condition_type, type = k.to_s.split('_').map{ |v| v.to_sym } if condition_type == :skip ## It is a "skip" condition skip_rules << ::StairMaster::Conditions::Skippable.new(self, type, v) else ## We don't know what this type of condition is so raise an exception raise RuntimeError, "Unknown type for: #{ k }" end end { skippable: skip_rules } end end end