Skip to content

Commit

Permalink
remove to_a [#73 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlo Cabanilla authored and andreas committed Nov 16, 2009
1 parent 8c0cdd9 commit c943911
Show file tree
Hide file tree
Showing 21 changed files with 159 additions and 153 deletions.
2 changes: 1 addition & 1 deletion examples/you_might_know/you_might_know.rb
Expand Up @@ -9,7 +9,7 @@
# The idea of the solution presented here is to keep track of the features that are common in each branch of the traversal
# and to stop when there is nothing in common any more or the maximum distance has been reached.
def you_might_know(node, matches, max_distance)
buddies = node.relationships.both(:knows).nodes.to_a
buddies = [*node.relationships.both(:knows).nodes]
find_friends(node, [node], matches, 1, max_distance, buddies)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/neo4j/extensions/aggregate/ext/node_mixin.rb
Expand Up @@ -20,7 +20,7 @@ module NodeMixin
# agg2 = MyNode
# agg2.aggregate([node1,node2]).group_by(:age)
#
# node1.aggregates.to_a # => [agg1, agg2]
# [*node1.aggregates] # => [agg1, agg2]
#
def aggregates
Neo4j::Aggregate::AggregateEnum.new(self)
Expand Down Expand Up @@ -55,11 +55,11 @@ def aggregates
# agg1 << node1
# agg2 << node1
#
# node1.aggregate_groups.to_a # => [agg1[some_group], agg2[some_other_group]]
# [*node1.aggregate_groups] # => [agg1[some_group], agg2[some_other_group]]
#
def aggregate_groups(group = :all)
return relationships.incoming(:aggregate).nodes if group == :all
relationships.incoming(:aggregate).filter{self[:aggregate_group] == group.to_s}.nodes.to_a[0]
[*relationships.incoming(:aggregate).filter{self[:aggregate_group] == group.to_s}.nodes][0]
end

end
Expand Down
28 changes: 14 additions & 14 deletions lib/neo4j/extensions/aggregate/node_aggregate_mixin.rb
Expand Up @@ -43,7 +43,7 @@ module Neo4j::Aggregate
#
# Get an enumeration of names of people having favorite colour 'red'
#
# a[:red][:name].to_a => ['bertil', 'adam', 'adam']
# [*a[:red][:name]] => ['bertil', 'adam', 'adam']
#
# ==== Example - group by a property value which is transformed
#
Expand Down Expand Up @@ -76,7 +76,7 @@ module Neo4j::Aggregate
# agg_node.aggregate([node1, node2]).group_by(:colour, :type)
#
# # node1 is member of two groups, red and A
# node1.aggregate_groups.to_a # => [agg_node[:red], agg_node[:A]]
# [*node1.aggregate_groups] # => [agg_node[:red], agg_node[:A]]
#
# # group A contains node1
# agg_node[:A].include?(node1) # => true
Expand Down Expand Up @@ -138,19 +138,19 @@ module Neo4j::Aggregate
# Neo4j::Transaction.run { blue_node = MyNode.new; a.colour = 'blue' }
# # then the aggregate will be updated automatically since it listen to property change events
# a['blue'].size = 1
# a['blue'].to_a[0] # => blue_node
# [*a['blue']][0] # => blue_node
#
# blue_node[:colour] = 'red'
# a['blue'] # => nil
# a['red'].to_a # => [blue_node]
# [*a['red']] # => [blue_node]
# blue_node.delete
# a['red'] # => nil
#
#
# ===== TODO Only Aggregate ???
#
# a.aggregate([n1,n2])
# a.to_a => [n1, n2]
# [*a] => [n1, n2]
#
# OR without aggregate method
# a << n1 << n2
Expand All @@ -167,9 +167,9 @@ module Neo4j::Aggregate
# n2 = [jan=>2, feb=>0, mars=>2, apr=>10, ...]
#
# a.aggregate_each([n1,n2]).group(:jan,:feb,:mars).by(:q1)
# a[:q1].to_a = [g1,g2]
# [*a[:q1]] = [g1,g2]
# g1.props => [n1.neo_node_id, jan=>1, feb=>5, ..., dec=>]
# g1.to_a => [1,5,2]
# [*g1] => [1,5,2]
#
# ===== Example Group by Each (2)
#
Expand All @@ -179,10 +179,10 @@ module Neo4j::Aggregate
#
# a.aggregate_each([n1,n2,n3]).group_by(:colour, :age)
# n1.aggregate_groups = [g1]
# g1.to_a = ['red', 10]
# a.to_a => [g1,g2,g3]
# [*g1] = ['red', 10]
# [*a] => [g1,g2,g3]
#
# g2.to_a = ['blue', 11]
# [*g2] = ['blue', 11]
# g1.props => [
#
#
Expand All @@ -196,11 +196,11 @@ module Neo4j::Aggregate
# n1 = [jan=>1, feb=>5, mars=>2, apr=>10, ...]
# n2 = [jan=>2, feb=>0, mars=>2, apr=>10, ...]
#
# q1.to_a => [g1,g2]
# g1.to_a => [1,5,2]
# g2.to_a => [2,0,2]
# [*q1] => [g1,g2]
# [*g1] => [1,5,2]
# [*g2] => [2,0,2]
#
# q2.to_a => [g3,g4]
# [*q2] => [g3,g4]
# n1.aggregate_groups = [g1,g2]
# n1[:q1] => nil OR [1,5,2] ????
# a[:q1] => [1,5,2,2,0,2]
Expand Down
10 changes: 8 additions & 2 deletions lib/neo4j/extensions/aggregate/node_aggregator.rb
Expand Up @@ -89,7 +89,7 @@ def on_prop_deleted(node, curr_node_values, old_node_values) # :nodoc:
removed = old_group_keys - new_group_keys

removed.each do |key|
member_of = node.relationships.incoming(:aggregate).filter{self[:aggregate_group] == key}.to_a
member_of = [*node.relationships.incoming(:aggregate).filter{self[:aggregate_group] == key}]
raise "same group key used in several aggregate groups, strange #{member_of.size}" if member_of.size > 1
next if member_of.empty?
group_node = member_of[0].start_node
Expand Down Expand Up @@ -148,7 +148,13 @@ def group_key_of(node)


# check all values and expand enumerable values
group_keys = values.inject(Set.new) {|result, value| value.respond_to?(:to_a) ? result.merge(value.to_a) : result << value }.to_a
group_keys = [*values.inject(Set.new) do |result, value|
if value.respond_to?(:to_a)
result.merge([*value])
else
result << value
end
end]

# if we are not grouping by_each then there will only be one group_key - join it
group_keys = [group_keys] unless group_keys.respond_to?(:each)
Expand Down
6 changes: 3 additions & 3 deletions lib/neo4j/extensions/rest/rest_mixin.rb
Expand Up @@ -86,7 +86,7 @@ def find(query=nil, &block)
if query[:search]
# Use Lucene
results = super(query[:search])
results = apply_lucene_sort(query[:sort], results).to_a rescue super(query[:search]).to_a
results = [*apply_lucene_sort(query[:sort], results)] rescue [*super(query[:search])]

else
# Use traverser
Expand Down Expand Up @@ -129,7 +129,7 @@ def apply_traverser_conditions(query)
def apply_ruby_sort(sort_string, results)
if sort_string
sort_fields = sort_string.to_s.split(/,/)
results.to_a.sort do |x,y|
[*results].sort do |x,y|
catch(:item_order) do
sort_fields.each_index do |index|
field = sort_fields[index]
Expand All @@ -146,7 +146,7 @@ def apply_ruby_sort(sort_string, results)
end
end
else
results.to_a
[*results]
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/neo4j/mixins/node_mixin.rb
Expand Up @@ -876,7 +876,7 @@ def has_one(rel_type, params = {})

module_eval(%Q{def #{rel_type}
r = Relationships::HasN.new(self,'#{rel_type.to_s}', #{cascade_delete})
r.to_a[0]
[*r][0]
end}, __FILE__, __LINE__)
relationships_info[rel_type] = Relationships::RelationshipInfo.new
end
Expand Down Expand Up @@ -953,7 +953,7 @@ def cascade_delete_param(params)
#
# employee2.delete
#
# company.employees.to_a # => [employee1, employee3]
# [*company.employees] # => [employee1, employee3]
# company.employees.size # => 2
#
# ===== List Items Memberships
Expand Down
26 changes: 13 additions & 13 deletions test/extensions/aggregate/node_aggregate_spec.rb
Expand Up @@ -23,8 +23,8 @@ class MyNode
group.relationships.outgoing(:foo) << node1
group.relationships.outgoing(:foo) << node2

group[:name].to_a.should include('node1', 'node2')
group[:name].to_a.size.should == 2
[*group[:name]].should include('node1', 'node2')
[*group[:name]].size.should == 2
Neo4j::Transaction.finish
end
end
Expand Down Expand Up @@ -424,14 +424,14 @@ class MyNode
@all = @red + @blue + @green
# create names a,b,c,d,a,b.c,d,a,b,c,d, ....
names = []
4.times { names += ('a' .. 'd').to_a}
4.times { names += [*('a' .. 'd')]}
@all.each {|n| n[:name] = names.pop}
@agg_node = NodeAggregate.new
@agg_node.aggregate(@all).group_by(:colour).execute
end

it "should create one group for each unique property value" do
@agg_node.to_a.size.should == 3
[*@agg_node].size.should == 3
end

it "should create groups with properties aggregate_group" do
Expand All @@ -450,21 +450,21 @@ class MyNode
end

it "should aggregate properties on aggregated nodes, e.g find name of all people having favorite colour 'red'" do
names = @agg_node[:red][:name].to_a
names = [*@agg_node[:red][:name]]
names.size.should == 5 # duplicated name 'd'
names.should include('a', 'b', 'c', 'd')
end

it "should not add nodes to the aggregation that does not have a group property" do
# add a node that does not have the colour property
@agg_node.to_a.size.should == 3
[*@agg_node].size.should == 3
@agg_node[:red].aggregate_size.should == 5
@agg_node[:blue].aggregate_size.should == 4
@agg_node[:green].aggregate_size.should == 3

@agg_node << Neo4j::Node.new

@agg_node.to_a.size.should == 3
[*@agg_node].size.should == 3
@agg_node[:red].aggregate_size.should == 5
@agg_node[:blue].aggregate_size.should == 4
@agg_node[:green].aggregate_size.should == 3
Expand All @@ -488,8 +488,8 @@ class MyNode
agg_node['B'].should include(node2)
agg_node['red'].should include(node1, node2)

node1.aggregate_groups.to_a.size.should == 2
node2.aggregate_groups.to_a.size.should == 2
[*node1.aggregate_groups].size.should == 2
[*node2.aggregate_groups].size.should == 2
end
end

Expand Down Expand Up @@ -519,8 +519,8 @@ class MyNode
@aggregate_node['young'].should include(@people[0], @people[1], @people[2], @people[3])
@aggregate_node['old'].should include(@people[4], @people[5])

@aggregate_node[0].to_a.size.should == 2
@aggregate_node[2].to_a.size.should == 1
[*@aggregate_node[0]].size.should == 2
[*@aggregate_node[2]].size.should == 1
@aggregate_node[2].should include(@people[5])
end

Expand All @@ -530,8 +530,8 @@ class MyNode

# then
@aggregate_node[0].should include(@people[0], @people[1])
@aggregate_node[0].to_a.size.should == 2
@aggregate_node[2].to_a.size.should == 1
[*@aggregate_node[0]].size.should == 2
[*@aggregate_node[2]].size.should == 1
@aggregate_node[2].should include(@people[5])
end

Expand Down
12 changes: 6 additions & 6 deletions test/extensions/aggregate/node_mixin_spec.rb
Expand Up @@ -33,7 +33,7 @@
agg_node.aggregate([node]).group_by(:colour).execute

node.aggregates.first.should == agg_node
node.aggregates.to_a.size.should == 1
[*node.aggregates].size.should == 1
end

it "returns two aggregates when one nodes belongs to two aggregates" do
Expand All @@ -47,7 +47,7 @@
agg_node2.aggregate([node]).group_by(:colour).execute

node.aggregates.should include(agg_node1, agg_node2)
node.aggregates.to_a.size.should == 2
[*node.aggregates].size.should == 2
end

end
Expand All @@ -65,7 +65,7 @@

agg_node.aggregate([node]).group_by(:colour).execute

node.aggregate_groups.to_a.size.should == 1
[*node.aggregate_groups].size.should == 1
node.aggregate_groups.first.should be_kind_of(Neo4j::Aggregate::NodeGroup)
end

Expand All @@ -77,9 +77,9 @@

agg_node.aggregate([node]).group_by(:colour, :name).execute

node.aggregate_groups.to_a.size.should == 2
node.aggregate_groups.to_a[0].should be_kind_of(Neo4j::Aggregate::NodeGroup)
node.aggregate_groups.to_a[1].should be_kind_of(Neo4j::Aggregate::NodeGroup)
[*node.aggregate_groups].size.should == 2
[*node.aggregate_groups][0].should be_kind_of(Neo4j::Aggregate::NodeGroup)
[*node.aggregate_groups][1].should be_kind_of(Neo4j::Aggregate::NodeGroup)
end

it "should return the given group if found" do
Expand Down
2 changes: 1 addition & 1 deletion test/extensions/aggregate/props_aggregate_spec.rb
Expand Up @@ -133,7 +133,7 @@

# then
q1.should include("andreas", "blue", "kalle")
q1.to_a.size.should == 4
[*q1].size.should == 4
end

it "should allow to create different aggregate groups from the same aggregate node" do
Expand Down
8 changes: 4 additions & 4 deletions test/extensions/graph_algo_spec.rb
Expand Up @@ -38,16 +38,16 @@
it "should contain Enumerable of Enumerable" do
paths = GraphAlgo.all_simple_paths.from(@n1).outgoing(:knows).to(@n5).depth(2)
paths.size.should == 2
paths.to_a[0].should be_kind_of Enumerable
paths.to_a[1].should be_kind_of Enumerable
[*paths][0].should be_kind_of Enumerable
[*paths][1].should be_kind_of Enumerable
end

it "should contain Enumerable of Enumerable of alternating Relationship and Nodes" do
paths = GraphAlgo.all_simple_paths.from(@n1).outgoing(:knows).to(@n5).depth(2)
node = true
node_and_rel_ids = []

paths.to_a[0].each do | node_or_rel|
[*paths][0].each do | node_or_rel|
if node
node_or_rel.should be_kind_of Neo4j::Node
node_and_rel_ids << node_or_rel.neo_node_id
Expand All @@ -67,7 +67,7 @@
it "should contain Enumerable of Enumerable of only Nodes if as_nodes is given" do
paths = GraphAlgo.all_simple_paths.from(@n1).outgoing(:knows).to(@n5).depth(2).as_nodes
node_ids = []
paths.to_a[0].each do | node|
[*paths][0].each do | node|
node.should be_kind_of Neo4j::Node
node_ids << node.neo_node_id
end
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/tx_tracker_spec.rb
Expand Up @@ -178,10 +178,10 @@ def to_s
first = @tx_node_list.tx_nodes.first
first[:tx_finished].should == true

second = @tx_node_list.tx_nodes.to_a[1]
second = [*@tx_node_list.tx_nodes][1]
second[:tx_finished].should be_nil

third = @tx_node_list.tx_nodes.to_a[2]
third = [*@tx_node_list.tx_nodes][2]
third[:tx_finished].should be_nil
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/neo4j/has_list_spec.rb
Expand Up @@ -104,7 +104,7 @@ class ListNode
node2.delete

# then
list_node.items.to_a.size.should == 1
[*list_node.items].size.should == 1
end

it "should contain items after append one item to a list (#<<)" do
Expand Down Expand Up @@ -152,7 +152,7 @@ class ListNode
list.items.should include(a)
list.items.should include(b)
list.items.should be_kind_of(Enumerable)
list.items.to_a.size.should == 2
[*list.items].size.should == 2
end

end
Expand Down

0 comments on commit c943911

Please sign in to comment.