From 69a3755c5a93395fd2fdfd5bee00e6064d1670f8 Mon Sep 17 00:00:00 2001
From: Grzegorz Bizon <grzesiek.bizon@gmail.com>
Date: Tue, 7 Jun 2016 11:58:02 +0200
Subject: [PATCH] Add Ci config entry that implements Null Object

---
 lib/gitlab/ci/config/node/entry.rb          | 22 +++++++++++----------
 lib/gitlab/ci/config/node/null.rb           | 17 ++++++++++++++++
 spec/lib/gitlab/ci/config/node/null_spec.rb | 17 ++++++++++++++++
 3 files changed, 46 insertions(+), 10 deletions(-)
 create mode 100644 lib/gitlab/ci/config/node/null.rb
 create mode 100644 spec/lib/gitlab/ci/config/node/null_spec.rb

diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb
index 007585d4019..af92899af40 100644
--- a/lib/gitlab/ci/config/node/entry.rb
+++ b/lib/gitlab/ci/config/node/entry.rb
@@ -5,22 +5,28 @@ module Gitlab
         class Entry
           include Config::ValidationHelpers
 
-          attr_reader :value, :parent
+          attr_reader :value, :nodes, :parent
 
           def initialize(value, config, parent = nil)
             @value = value
             @config = config
             @parent = parent
-            @nodes = {}
-            @errors = []
+            @nodes, @errors = [], []
+
+            keys.each_key do |key|
+              instance_variable_set("@#{key}", Null.new(nil, config, self))
+            end
           end
 
           def process!
             return if leaf?
 
-            keys.each_pair do |key, entry|
-              next unless @value.include?(key)
-              @nodes[key] = entry.new(@value[key], @config, self)
+            keys.each do |key, entry_class|
+              next unless @value.has_key?(key)
+
+              entry = entry_class.new(@value[key], @config, self)
+              instance_variable_set("@#{key}", entry)
+              @nodes.append(entry)
             end
 
             nodes.each(&:process!)
@@ -31,10 +37,6 @@ module Gitlab
             @errors + nodes.map(&:errors).flatten
           end
 
-          def nodes
-            @nodes.values
-          end
-
           def valid?
             errors.none?
           end
diff --git a/lib/gitlab/ci/config/node/null.rb b/lib/gitlab/ci/config/node/null.rb
new file mode 100644
index 00000000000..6147b0d882f
--- /dev/null
+++ b/lib/gitlab/ci/config/node/null.rb
@@ -0,0 +1,17 @@
+module Gitlab
+  module Ci
+    class Config
+      module Node
+        class Null < Entry
+          def keys
+            {}
+          end
+
+          def method_missing(*)
+            nil
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/spec/lib/gitlab/ci/config/node/null_spec.rb b/spec/lib/gitlab/ci/config/node/null_spec.rb
new file mode 100644
index 00000000000..42a67892966
--- /dev/null
+++ b/spec/lib/gitlab/ci/config/node/null_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe Gitlab::Ci::Config::Node::Null do
+  let(:entry) { described_class.new(double, double) }
+
+  describe '#leaf?' do
+    it 'is leaf node' do
+      expect(entry).to be_leaf
+    end
+  end
+
+  describe '#any_method' do
+    it 'responds with nil' do
+      expect(entry.any_method).to be nil
+    end
+  end
+end
-- 
GitLab