diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-08 18:06:02 -0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-08 18:06:02 -0300 |
commit | 8f3b6591ccc1dcebb0e4ae1e3cf44588b2a4afe8 (patch) | |
tree | b470d8784d2e8e13b14a16aa9450a3676d520c33 | |
parent | cb8e9bb6bb0cb3712b888f79d2e8e324724742ad (diff) | |
download | gitlab-ce-gh-refactor.tar.gz |
Extract base mappergh-refactor
-rw-r--r-- | lib/gitlab/import/github/mapper/base.rb | 37 | ||||
-rw-r--r-- | lib/gitlab/import/github/mapper/label.rb | 31 | ||||
-rw-r--r-- | lib/gitlab/import/github/mapper/milestone.rb | 41 | ||||
-rw-r--r-- | spec/lib/gitlab/import/github/mapper/base_spec.rb | 19 | ||||
-rw-r--r-- | spec/lib/gitlab/import/github/mapper/label_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/import/github/mapper/milestone_spec.rb | 2 |
6 files changed, 85 insertions, 47 deletions
diff --git a/lib/gitlab/import/github/mapper/base.rb b/lib/gitlab/import/github/mapper/base.rb new file mode 100644 index 00000000000..78e8b97eb03 --- /dev/null +++ b/lib/gitlab/import/github/mapper/base.rb @@ -0,0 +1,37 @@ +module Gitlab + module Import + module Github + module Mapper + class Base + def initialize(project, client) + @project = project + @client = client + end + + def each + return enum_for(:each) unless block_given? + + method = klass.to_s.underscore.pluralize + + client.public_send(method).each do |raw| + yield(klass.new(attributes_for(raw))) + end + end + + private + + attr_reader :project, :client + + def attributes_for + {} + end + + def klass + raise NotImplementedError, + "#{self.class} does not implement #{__method__}" + end + end + end + end + end +end diff --git a/lib/gitlab/import/github/mapper/label.rb b/lib/gitlab/import/github/mapper/label.rb index 57c0cc35810..4a56a190d18 100644 --- a/lib/gitlab/import/github/mapper/label.rb +++ b/lib/gitlab/import/github/mapper/label.rb @@ -2,29 +2,20 @@ module Gitlab module Import module Github module Mapper - class Label - def initialize(project, client) - @project = project - @client = client - end - - def each - return enum_for(:each) unless block_given? - - client.labels.each do |raw| - label = ::Label.new( - project: project, - title: raw.name, - color: "##{raw.color}" - ) + class Label < Base + private - yield(label) - end + def attributes_for(raw) + { + project: project, + title: raw.name, + color: "##{raw.color}" + } end - private - - attr_reader :project, :client + def klass + ::Label + end end end end diff --git a/lib/gitlab/import/github/mapper/milestone.rb b/lib/gitlab/import/github/mapper/milestone.rb index a675defdbe9..4d6e8454c55 100644 --- a/lib/gitlab/import/github/mapper/milestone.rb +++ b/lib/gitlab/import/github/mapper/milestone.rb @@ -2,34 +2,25 @@ module Gitlab module Import module Github module Mapper - class Milestone - def initialize(project, client) - @project = project - @client = client - end - - def each - return enum_for(:each) unless block_given? - - client.milestones.each do |raw| - milestone = ::Milestone.new( - iid: raw.number, - project: project, - title: raw.title, - description: raw.description, - due_date: raw.due_on, - state: raw.state == 'closed' ? 'closed' : 'active', - created_at: raw.created_at, - updated_at: raw.state == 'closed' ? raw.closed_at : raw.updated_at - ) + class Milestone < Base + private - yield(milestone) - end + def attributes_for(raw) + { + iid: raw.number, + project: project, + title: raw.title, + description: raw.description, + due_date: raw.due_on, + state: raw.state == 'closed' ? 'closed' : 'active', + created_at: raw.created_at, + updated_at: raw.state == 'closed' ? raw.closed_at : raw.updated_at + } end - private - - attr_reader :project, :client + def klass + ::Milestone + end end end end diff --git a/spec/lib/gitlab/import/github/mapper/base_spec.rb b/spec/lib/gitlab/import/github/mapper/base_spec.rb new file mode 100644 index 00000000000..f3d823b19f2 --- /dev/null +++ b/spec/lib/gitlab/import/github/mapper/base_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe Gitlab::Import::Github::Mapper::Base, lib: true do + let(:project) { double } + let(:client) { double } + + subject(:mapper) do + klass = Class.new(described_class) + klass.new(project, client) + end + + describe '#each' do + context 'when klass is not implemented' do + it 'raises NotImplementedError' do + expect { mapper.each(&:to_s) }.to raise_error(NotImplementedError) + end + end + end +end diff --git a/spec/lib/gitlab/import/github/mapper/label_spec.rb b/spec/lib/gitlab/import/github/mapper/label_spec.rb index c02758cda44..94d810ef9f9 100644 --- a/spec/lib/gitlab/import/github/mapper/label_spec.rb +++ b/spec/lib/gitlab/import/github/mapper/label_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Gitlab::Import::Github::Mapper::Label, lib: true do - let(:project) { create(:empty_project) } + let(:project) { build(:empty_project) } let(:client) { double(labels: response) } let(:response) do diff --git a/spec/lib/gitlab/import/github/mapper/milestone_spec.rb b/spec/lib/gitlab/import/github/mapper/milestone_spec.rb index 9ebcf781424..ea580771bf7 100644 --- a/spec/lib/gitlab/import/github/mapper/milestone_spec.rb +++ b/spec/lib/gitlab/import/github/mapper/milestone_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Gitlab::Import::Github::Mapper::Milestone, lib: true do - let(:project) { create(:empty_project) } + let(:project) { build(:empty_project) } let(:client) { double(milestones: response) } let(:response) do |