Skip to content

refactor #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2015
Merged
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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
0.0.17 / 2015-07-07
========
* Refactor ListDifferenceBuilder
* Refactor ListDifferenceBuilder/OrderedListDifferenceBuilder to ListBuilder/OrderedListBuilder
* Name change from ListDifferenceBuilder to ListBuilder
* Breaking change, LDB now takes 2 arguments instead of 3 and the output of the #build method is an array of AR objects rather than a hash.
* Add CHANGELOG.md
* Remove deprecation on ListDifferenceBuilder
* Add to CHANGELOG.md

0.0.16 / 2015-06-29
========
Expand Down
3 changes: 1 addition & 2 deletions lib/deprecated_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module Xing
:ResourcesSerializer => Xing::Serializers::RootResources,
:JsonTreeLister => Xing::Services::JsonTreeLister,
:ActiveModelErrorConverter => Xing::Services::ErrorConverter,
:RemoteSnapshotFetcher => Xing::Services::SnapshotFetcher,
:ListDifferenceBuilder => Xing::Builders::OrderedListDifferenceBuilder
:RemoteSnapshotFetcher => Xing::Services::SnapshotFetcher
}
end

Expand Down
4 changes: 2 additions & 2 deletions lib/xing/builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module Builders
end
end

require 'xing/builders/list_difference_builder'
require 'xing/builders/ordered_list_difference_builder'
require 'xing/builders/list_builder'
require 'xing/builders/ordered_list_builder'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Xing
module Builders
class ListDifferenceBuilder
class ListBuilder
include Services::Locator

# list_data is is an array of JSON objects passed in by the mapper (the new list of records)
Expand All @@ -15,39 +15,27 @@ def initialize(list_data, mapper_class)

attr_reader :errors

def build
sort_json_items
map_items

@new_list
end

def sort_json_items
@list_data = @list_data.map do |data|
{ :locator => set_locator(data), :incoming => data}
end
end

def set_locator(data)
locator_for(data) unless (data[:links] || {})[:self].blank?
end

def locator_for(data)
route_to(data[:links][:self])[:id].to_i
route_to(data[:links][:self])[:id].to_i unless (data[:links] || {})[:self].blank?
end

def map_items
def build
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, that's the other simplification I identified. :-)

@new_list = []
@list_data.each_with_index do |item, index|
@list_data.each_with_index do |data, index|

mapper = @mapper_class.new(item[:incoming], item[:locator])
mapper = @mapper_class.new(data, locator_for(data))

# Sets association, attributes
mapper.perform_mapping
set_position(mapper.record, index)

@new_list << mapper.record
@errors[index] = mapper.errors[:data] unless mapper.errors[:data].blank?
end
@new_list
end

def set_position(record, index)
# position is not set in list builder
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/xing/builders/ordered_list_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Xing
module Builders
class OrderedListBuilder < ListBuilder
def set_position(record, index)
record.position = index if record.has_attribute?(:position)
end
end
end
end
24 changes: 0 additions & 24 deletions lib/xing/builders/ordered_list_difference_builder.rb

This file was deleted.

19 changes: 0 additions & 19 deletions spec/deprecated_classes/list_difference_builder_spec.rb

This file was deleted.

84 changes: 84 additions & 0 deletions spec/xing/builders/list_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'spec_helper'

describe Xing::Builders::ListBuilder do

let :builder do
Xing::Builders::ListBuilder.new(list_data, mapper_class)
end

let :mapper_class do
double("ItemMapper")
end

let :mapper_instance do
double("mapper")
end

let :new_ar_object do
double("New Relation AR Object")
end

let :updated_ar_object do
double("Updated AR Object")
end

let :list_data do
[
{
links: {
self: "/somethings/1"
},
data: {
stuff: "some updated stuff"
}
},
{
links: {
self: ""
},
data: {
stuff: "some new stuff"
}
}
]
end

it "initialize" do
expect(builder.instance_variable_get('@list_data')).to eq(list_data)
expect(builder.instance_variable_get('@mapper_class')).to eq(mapper_class)
expect(builder.instance_variable_get('@errors')).to eq({})
end

describe "#build" do
before :each do
allow(builder).to receive(:locator_for).and_return(1)
allow(mapper_class).to receive(:new).with(list_data[0], 1).and_return(mapper_instance)
allow(mapper_class).to receive(:new).with(list_data[1], 1).and_return(mapper_instance)
allow(mapper_instance).to receive(:record).and_return(new_ar_object, new_ar_object, updated_ar_object, updated_ar_object)
allow(mapper_instance).to receive(:perform_mapping)
allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
allow(updated_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
end

context "successful" do
before :each do
allow(mapper_instance).to receive(:errors).and_return({})
end

it "should return array of AR records" do
expect(builder.build).to match_array([new_ar_object, updated_ar_object])
end
end

context "with errors" do
before :each do
allow(mapper_instance).to receive(:errors).and_return({data: {type: "I would do anything for love", message: "but I won't do that"}}, {data: {type: "I would do anything for love", message: "but I won't do that"}}, {})
builder.build
end

it "should return errors" do
expect(builder.errors).to eq({0=>{:type=>"I would do anything for love", :message=>"but I won't do that"}})
end
end
end
end
164 changes: 0 additions & 164 deletions spec/xing/builders/list_difference_builder_spec.rb

This file was deleted.

Loading