lib/sinatra/rabbit/features.rb in sinatra-rabbit-1.0.1 vs lib/sinatra/rabbit/features.rb in sinatra-rabbit-1.0.2
- old
+ new
@@ -16,26 +16,37 @@
module Sinatra
module Rabbit
class Feature
attr_reader :name
+ attr_reader :description
attr_reader :collection
attr_reader :operations
+ attr_reader :constraints
def initialize(name, opts={}, &block)
@name = name
@operations = []
@collection = opts[:for]
+ @constraints = {}
raise "Each feature must define collection for which it will be valid using :for parameter" unless @collection
instance_eval(&block) if block_given?
end
def operation(name, &block)
@operations << Operation.new(name, &block) if block_given?
@operations.find { |o| o.name == name }
end
+ def description(s=nil)
+ @description ||= s
+ end
+
+ def constraint(name, value)
+ @constraints[name] = value
+ end
+
class Operation
attr_reader :name
attr_reader :params
def initialize(name, &block)
@@ -47,17 +58,23 @@
end
module Features
def features(&block)
- @@features ||= []
+ @features ||= []
instance_eval(&block) if block_given?
- @@features
+ @features
end
def feature(name, opts={}, &block)
- @@features << Feature.new(name, opts, &block) if block_given?
- @@features.find { |f| f.name == name }
+ feature = @features.find { |f| f.name == name }
+ return feature unless block_given?
+ if feature
+ feature.class_eval(&block)
+ else
+ @features << Feature.new(name, opts, &block) if block_given?
+ end
+ @features.find { |f| f.name == name }
end
def self.included(base)
base.register(Features)
end