Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/smart_properties/property_collection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'forwardable'

module SmartProperties
class PropertyCollection
include Enumerable
extend Forwardable

attr_reader :parent

Expand Down Expand Up @@ -45,17 +48,17 @@ def keys
collection_with_parent_collection.keys.map(&:to_sym)
end

def values
collection_with_parent_collection.values
end

def each(&block)
return to_enum(:each) if block.nil?
collection_with_parent_collection.each { |name, value| block.call([name.to_sym, value]) }
end

def to_h
each.to_h
end

def to_hash
Hash[each.to_a]
to_h
end

def register(child)
Expand All @@ -64,6 +67,8 @@ def register(child)
nil
end

def_delegators :collection_with_parent_collection, :count, :size, :length, :values

protected

attr_accessor :children
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@
expect(base_class.new).to have_smart_property(:title)
end.not_to raise_error(SystemStackError)
end

specify "PropertyCollection has O(1) #count" do
property_collection = Class.new(SmartProperties::PropertyCollection) do
def collection_with_parent_collection
mock('collection').expects(:count).once.returns(42)
end
end

expect(property_collection.new.count).to eq(42)
end
end