Skip to content

Commit

Permalink
finishing test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kerinin committed May 8, 2012
1 parent 3be5622 commit e30823e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/daggregator/model/class_methods.rb
Expand Up @@ -5,7 +5,7 @@ def aggregate_to(type=self.name)
type = type.to_s

@daggregator_options ||= {}
daggregator_options[type] ||= Daggregator::Model::GraphBuilder.new
daggregator_options[type] ||= Daggregator::Model::GraphBuilder.new(type)
yield daggregator_options[type]
end
end
14 changes: 11 additions & 3 deletions lib/daggregator/model/graph_builder.rb
@@ -1,8 +1,14 @@
class Daggregator::Model::GraphBuilder
attr_reader :identifier_proc, :keys, :flows

def initialize
@identifier_proc = Proc.new {|instance| "#{instance.class.name}_#{instance.id}" }
def initialize(type = nil)
unless type.nil?
# Default to using the node type as the prefix
@identifier_proc = Proc.new {|instance| "#{type}_#{instance.id}" }
else
# If none specified for some reason, use the instance class name
@identifier_proc = Proc.new {|instance| "#{instance.class.name}_#{instance.id}" }
end
@keys = {}
@flows = {}
end
Expand Down Expand Up @@ -34,12 +40,14 @@ def key(key_name, args={}, &block)
@keys[key_name] = block
end

# flows[association name on model] = [node type on associated]
#
def flow_to(association_name, args={})
association_name = association_name.to_s
# RM NOTE: you should probably set up identifier for the target class
# if it isn't already

@flows[association_name] ||= []
@flows[association_name] << ( args[:as] || association_name ).to_s
@flows[association_name] << ( args[:as] || :default ).to_s
end
end
16 changes: 10 additions & 6 deletions lib/daggregator/model/serialization.rb
Expand Up @@ -10,15 +10,21 @@ def to_node_for(type)
def to_flows_for(type)
type = type.to_s
flows = []
daggregator_options[type].flows.each_key do |association_name|
daggregator_options[type].flows.each_pair do |association_name, related_types|
# association_name is the model association which returns related instances
flows += send(association_name).map do |associated|
associated_identifiers_for(type, association_name)
# Construct the identifiers for related instance `associated`, given node types
associated_identifiers_for(associated, related_types)
end
end
flows.flatten.uniq
end

def identifier_for(type)
type = type.to_s
if type == 'default'
type = self.class.name
end
instance_eval &self.class.daggregator_options[type].identifier_proc
end

Expand All @@ -29,10 +35,8 @@ def daggregator_options
self.class.daggregator_options
end

def associated_identifiers_for(type, association_name)
type = type.to_s
associated_types = self.class.daggregator_options[type].flows[association_name]
associated_types.map {|association_name| send(type).identifier_for(type) }
def associated_identifiers_for(associated, related_types)
related_types.map {|type| associated.identifier_for(type) }
end

def node_data_for(type)
Expand Down
5 changes: 3 additions & 2 deletions spec/daggregator/model/graph_builder_spec.rb
Expand Up @@ -48,9 +48,10 @@
subject.flows['foo'].should be_a(Array)
end

it "defaults to key" do
it "defaults to default" do
# NOTE: this uses the node type named after the target's class name
subject.flow_to(:foo)
subject.flows['foo'].should include('foo')
subject.flows['foo'].should include('default')
end

it "creates flow with :as" do
Expand Down
49 changes: 45 additions & 4 deletions spec/daggregator/model/serialization_spec.rb
Expand Up @@ -11,8 +11,12 @@ def initialize(id=3)
@id = id
end

def property
'value'
end

def associated
[1..3].map {|i| TestClass.new(i) }
(1..3).to_a.map {|i| TestClass.new(i) }
end
end
end
Expand Down Expand Up @@ -63,12 +67,49 @@ def associated
end

describe "identifier_for" do
it "evaluates identifier proc in instance's scope"
before(:each) do
TestClass.aggregate_to do |node|
node.identifier(:foo)
end
end
subject { TestClass.new(3) }

it "evaluates identifier proc in instance's scope" do
subject.identifier_for('TestClass').should == 'foo_3'
end
end

describe "node_data_for" do
it "returns a hash for each key"
before(:each) do
TestClass.aggregate_to do |node|
node.key :property
node.key :foo, :from => :property
node.key :bar do
property
end
end
TestClass.aggregate_to(:custom) do |node| end;
end
subject { TestClass.new(3) }

it "returns a hash for default type" do
subject.node_data_for('TestClass').should be_a(Hash)
end

it "returns a hash for specific types" do
subject.node_data_for(:custom).should be_a(Hash)
end

it "calls each key's proc in the instance's scope"
it "calls procs for default keys" do
subject.node_data_for('TestClass')['property'].should == 'value'
end

it "calls procs for renamed keys" do
subject.node_data_for('TestClass')['foo'].should == 'value'
end

it "calls custom procs" do
subject.node_data_for('TestClass')['bar'].should == 'value'
end
end
end
2 changes: 1 addition & 1 deletion spec/daggregator/model_spec.rb
Expand Up @@ -53,7 +53,7 @@
end

it "creates flows for renamed associations" do
subject.to_flows_for('TestModel').should include('two_1')
subject.to_flows_for('TestModel').should include('identifier_two')
end
end
end
2 changes: 1 addition & 1 deletion spec/test_model.rb
Expand Up @@ -45,7 +45,7 @@ def property_2
end

def fake_association
[0..3].map {|i| TestModel.new(i) }
(0..3).to_a.map {|i| TestModel.new(i) }
end

def two
Expand Down

0 comments on commit e30823e

Please sign in to comment.