lib/eco/api/usecases/use_group.rb in eco-helpers-0.6.17 vs lib/eco/api/usecases/use_group.rb in eco-helpers-0.7.1
- old
+ new
@@ -1,13 +1,14 @@
module Eco
module API
module UseCases
class UseGroup
+ include Enumerable
def initialize()
- @usecases = {}
- @cache_init = false
+ @usecases = {}
+ @cache_init = false
@cases_by_name = {}
end
def add(usecase)
raise "Expected UseCase object. Given: #{usecase}" if !usecase || !usecase.is_a?(UseCase)
@@ -18,11 +19,13 @@
@usecases[key(name, type)] = usecase
usecase
end
def define(name, type:, &block)
- add(UseCase.new(name, type: type, root:self, &block))
+ UseCase.new(name, type: type, root: self, &block).tap do |usecase|
+ add(usecase)
+ end
end
def defined?(name, type: nil)
return @usecases.key?(key(name,type)) if type
name?(name)
@@ -52,11 +55,11 @@
# merges cases overriding self for exisint parsers
def merge(cases)
return self if !cases
raise "Expected a UseGroup object. Given #{cases}" if !cases.is_a?(UseGroup)
- cases_hash = cases.hash
+ cases_hash = cases.to_h
@usecases.merge!(cases_hash)
cases_hash.transform_values do |usecase|
usecase.root = self
@@ -64,12 +67,29 @@
@cache_init = false
self
end
+ def length
+ count
+ end
+
+ def empty?
+ count == 0
+ end
+
+ def each(params: {}, &block)
+ return to_enum(:each) unless block
+ items.each(&block)
+ end
+
+ def items
+ @usecases.values
+ end
+
protected
- def hash
+ def to_h
@usecases
end
private