pakyow-presenter/lib/presenter/view_collection.rb in pakyow-presenter-0.9.1 vs pakyow-presenter/lib/presenter/view_collection.rb in pakyow-presenter-0.10.0

- old
+ new

@@ -1,14 +1,25 @@ module Pakyow module Presenter class ViewCollection include Enumerable - def initialize + attr_reader :views, :scoped_as + + def initialize(scope = nil) @views = [] + @scoped_as = scope end + def ==(other) + @views.each_with_index do |view, i| + return false if view != other.views[i] + end + + return true + end + def each @views.each { |v| yield(v) } end def attrs(attrs = {}) @@ -78,31 +89,84 @@ def length @views.length end def scope(name) - inject(ViewCollection.new) { |coll, view| + collection = inject(ViewCollection.new(name)) { |coll, view| scopes = view.scope(name) next if scopes.nil? scopes.inject(coll) { |coll, scoped_view| coll << scoped_view } } + + if collection.versioned? + ViewVersion.new(collection.views) + else + collection + end end def prop(name) - inject(ViewCollection.new) { |coll, view| + inject(ViewCollection.new(scoped_as)) { |coll, view| scopes = view.prop(name) next if scopes.nil? scopes.inject(coll) { |coll, scoped_view| coll << scoped_view } } end + def versioned? + each do |view| + return true if view.versioned? + end + + false + end + + def exists? + each do |view| + return true if view.exists? + end + + false + end + + def component(name) + collection = inject(ViewCollection.new(scoped_as)) { |coll, view| + scopes = view.component(name) + next if scopes.nil? + + scopes.inject(coll) { |coll, scoped_view| + coll << scoped_view + } + } + + if collection.versioned? + ViewVersion.new(collection.views) + else + collection + end + end + + def component? + each do |view| + return true if view.component? + end + + false + end + + def component_name + each do |view| + return view.component_name if view.component? + end + end + # call-seq: # with {|view| block} # # Creates a context in which view manipulations can be performed. # @@ -135,10 +199,12 @@ view.instance_exec(data[i], &block) else block.call(view, data[i]) end end + + self end # call-seq: # for_with_index {|view, datum, i| block} # @@ -164,25 +230,26 @@ # # Manipulates the current collection to match the data. The final ViewCollection object # will consist n copies of self[data index] || self[-1], where n = data.length. # def match(data) + return self if length == 0 data = Array.ensure(data) # an empty set always means an empty view if data.empty? remove else - original_view = self[-1].soft_copy if data.length > length - if length > data.length self[data.length..-1].each do |view| view.remove end else + working = self[-1] data[length..-1].each do - duped_view = original_view.soft_copy - self[-1].after(duped_view) + duped_view = working.soft_copy + working.after(duped_view) + working = duped_view self << duped_view end end end