From c2aa17700c5ed6d8ceb954afcd93ad17c415300e Mon Sep 17 00:00:00 2001 From: Scott Bellware Date: Wed, 22 May 2019 01:26:52 -0400 Subject: [PATCH 1/2] Exclude is not offered by data source interface --- lib/set_attributes/data_source.rb | 8 +++----- test/automated/data_source/{exclude.rb => _exclude.rb} | 0 2 files changed, 3 insertions(+), 5 deletions(-) rename test/automated/data_source/{exclude.rb => _exclude.rb} (100%) diff --git a/lib/set_attributes/data_source.rb b/lib/set_attributes/data_source.rb index 79d1abd..38dbe72 100644 --- a/lib/set_attributes/data_source.rb +++ b/lib/set_attributes/data_source.rb @@ -18,10 +18,6 @@ def initialize(source, attribute_map) @attribute_map = attribute_map end - def exclude(*attributes) - attribute_map.exclude(attributes) - end - module Build def build(source, include=nil, exclude: nil) include = assure_include(source, include) @@ -35,7 +31,9 @@ def build(source, include=nil, exclude: nil) attribute_map = SetAttributes::Map.build(include) instance = new(source, attribute_map) - instance.exclude(exclude) + + attribute_map.exclude(exclude) + instance end end diff --git a/test/automated/data_source/exclude.rb b/test/automated/data_source/_exclude.rb similarity index 100% rename from test/automated/data_source/exclude.rb rename to test/automated/data_source/_exclude.rb From a3e62a89634ed0bbd4e0618db49687f3fead639b Mon Sep 17 00:00:00 2001 From: Scott Bellware Date: Wed, 22 May 2019 01:34:08 -0400 Subject: [PATCH 2/2] Data source constructor returns data source and attribute map --- lib/set_attributes/data_source.rb | 10 +++---- lib/set_attributes/set_attributes.rb | 10 ++++--- .../data_source/excluded_from_build.rb | 12 ++++---- test/automated/data_source/factory/hash.rb | 2 +- test/automated/data_source/factory/object.rb | 2 +- test/automated/data_source/hash/get_value.rb | 28 ++++--------------- ...t_value_from_attribute_that_isnt_mapped.rb | 4 +-- .../data_source/hash/include_is_omitted.rb | 6 ++-- .../automated/data_source/object/get_value.rb | 28 ++++--------------- ...t_value_from_attribute_that_isnt_mapped.rb | 6 ++-- 10 files changed, 38 insertions(+), 70 deletions(-) diff --git a/lib/set_attributes/data_source.rb b/lib/set_attributes/data_source.rb index 38dbe72..c5b2970 100644 --- a/lib/set_attributes/data_source.rb +++ b/lib/set_attributes/data_source.rb @@ -13,9 +13,8 @@ def self.assure_include(source, include) attr_reader :source attr_reader :attribute_map - def initialize(source, attribute_map) + def initialize(source) @source = source - @attribute_map = attribute_map end module Build @@ -29,12 +28,11 @@ def build(source, include=nil, exclude: nil) exclude = Array(exclude) attribute_map = SetAttributes::Map.build(include) - - instance = new(source, attribute_map) - attribute_map.exclude(exclude) - instance + instance = new(source) + + return instance, attribute_map end end diff --git a/lib/set_attributes/set_attributes.rb b/lib/set_attributes/set_attributes.rb index 53aa9d6..c119871 100644 --- a/lib/set_attributes/set_attributes.rb +++ b/lib/set_attributes/set_attributes.rb @@ -1,15 +1,17 @@ class SetAttributes attr_reader :receiver attr_reader :data_source + attr_reader :attribute_map def strict @strict ||= false end attr_writer :strict - def initialize(receiver, data_source) + def initialize(receiver, data_source, attribute_map) @receiver = receiver @data_source = data_source + @attribute_map = attribute_map end def self.build(receiver, source, copy: nil, include: nil, exclude: nil, strict: nil) @@ -19,9 +21,9 @@ def self.build(receiver, source, copy: nil, include: nil, exclude: nil, strict: include = copy end - data_source = SetAttributes::DataSource.build_data_source(source, include, exclude: exclude) + data_source, attribute_map = SetAttributes::DataSource.build_data_source(source, include, exclude: exclude) - new(receiver, data_source).tap do |instance| + new(receiver, data_source, attribute_map).tap do |instance| instance.strict = strict end end @@ -34,7 +36,7 @@ def self.call(receiver, source, include: nil, copy: nil, exclude: nil, strict: n def call set_attributes = [] - data_source.attribute_map.each_mapping do |source_attribute, receiver_attribute| + attribute_map.each_mapping do |source_attribute, receiver_attribute| value = data_source.get_value(source_attribute) Assign.(receiver, receiver_attribute, value, strict: strict) set_attributes << receiver_attribute diff --git a/test/automated/data_source/excluded_from_build.rb b/test/automated/data_source/excluded_from_build.rb index 027bfa5..b07ae03 100644 --- a/test/automated/data_source/excluded_from_build.rb +++ b/test/automated/data_source/excluded_from_build.rb @@ -3,23 +3,23 @@ context "Data Source" do context "Excluded from Build" do context "Exclude One Mapping" do - data_source = SetAttributes::Controls::DataSource.example(exclude: [:some_other_attribute]) + _, attribute_map = SetAttributes::Controls::DataSource.example(exclude: [:some_other_attribute]) test "Mapped attributes are included" do - assert(data_source.attribute_map.include?(:some_attribute)) + assert(attribute_map.include?(:some_attribute)) end test "Excluded mapping is removed from the attribute map" do - refute(data_source.attribute_map.include?(:some_other_attribute)) + refute(attribute_map.include?(:some_other_attribute)) end end context "Exclude Many Mappings" do - data_source = SetAttributes::Controls::DataSource.example(exclude: [:some_attribute, :some_other_attribute]) + _, attribute_map = SetAttributes::Controls::DataSource.example(exclude: [:some_attribute, :some_other_attribute]) test "Excluded mappings are removed from the attribute map" do - refute(data_source.attribute_map.include?(:some_attribute)) - refute(data_source.attribute_map.include?(:some_other_attribute)) + refute(attribute_map.include?(:some_attribute)) + refute(attribute_map.include?(:some_other_attribute)) end end end diff --git a/test/automated/data_source/factory/hash.rb b/test/automated/data_source/factory/hash.rb index 75071da..3508b6c 100644 --- a/test/automated/data_source/factory/hash.rb +++ b/test/automated/data_source/factory/hash.rb @@ -5,7 +5,7 @@ context "Hash" do source = Controls::Hash.example - data_source = SetAttributes::DataSource.build_data_source(source) + data_source, _ = SetAttributes::DataSource.build_data_source(source) test "Is a hash data source" do assert(data_source.is_a?(SetAttributes::DataSource::Hash)) diff --git a/test/automated/data_source/factory/object.rb b/test/automated/data_source/factory/object.rb index 3803b44..3e25d74 100644 --- a/test/automated/data_source/factory/object.rb +++ b/test/automated/data_source/factory/object.rb @@ -7,7 +7,7 @@ placeholder_mapping = [] - data_source = SetAttributes::DataSource.build_data_source(source, placeholder_mapping) + data_source, _ = SetAttributes::DataSource.build_data_source(source, placeholder_mapping) test "Is an object data source" do assert(data_source.is_a?(SetAttributes::DataSource::Object)) diff --git a/test/automated/data_source/hash/get_value.rb b/test/automated/data_source/hash/get_value.rb index ebd7ad7..355c063 100644 --- a/test/automated/data_source/hash/get_value.rb +++ b/test/automated/data_source/hash/get_value.rb @@ -3,32 +3,16 @@ context "Data Source" do context "Hash" do context "Get Value" do - context "Source Attribute Name and Receiver Attribute Name Are the Same" do - mapping = Controls::Mapping::Unbalanced.example + mapping = Controls::Mapping.example - data = SetAttributes::Controls::Hash.example + source = SetAttributes::Controls::Hash.example - hash_source = SetAttributes::DataSource::Hash.build(data, mapping) + hash_source, _ = SetAttributes::DataSource::Hash.build(source, mapping) - value = hash_source.get_value(:some_attribute) + value = hash_source.get_value(:some_attribute) - test "Source value is retrieved by the receiver attribute name" do - assert(value = data[:some_attribute]) - end - end - - context "Source Attribute Name and Receiver Attribute Name Are Not the Same" do - mapping = Controls::Mapping::Transform.example - - data = SetAttributes::Controls::Hash.example - - hash_source = SetAttributes::DataSource::Hash.build(data, mapping) - - value = hash_source.get_value(:some_attribute) - - test "Source value is retrieved by the receiver attribute name" do - assert(value = data[:some_other_attribute]) - end + test "Source value is retrieved by indexer and key" do + assert(value = source[:some_attribute]) end end end diff --git a/test/automated/data_source/hash/get_value_from_attribute_that_isnt_mapped.rb b/test/automated/data_source/hash/get_value_from_attribute_that_isnt_mapped.rb index 8a6891b..5008034 100644 --- a/test/automated/data_source/hash/get_value_from_attribute_that_isnt_mapped.rb +++ b/test/automated/data_source/hash/get_value_from_attribute_that_isnt_mapped.rb @@ -3,11 +3,11 @@ context "Data Source" do context "Hash" do context "Get Value from Source Attribute that Isn't Mapped" do - mapping = Controls::Mapping::Unbalanced.example + mapping = nil data = SetAttributes::Controls::Hash.example - hash_source = SetAttributes::DataSource::Hash.build(data, mapping) + hash_source, _ = SetAttributes::DataSource::Hash.build(data, mapping) value = hash_source.get_value(Controls::Attribute::Random.example) diff --git a/test/automated/data_source/hash/include_is_omitted.rb b/test/automated/data_source/hash/include_is_omitted.rb index 96c0b5c..1ca3206 100644 --- a/test/automated/data_source/hash/include_is_omitted.rb +++ b/test/automated/data_source/hash/include_is_omitted.rb @@ -3,12 +3,12 @@ context "Data Source" do context "Hash" do context "Include is Omitted" do - data = SetAttributes::Controls::Hash.example + source = SetAttributes::Controls::Hash.example - hash_source = SetAttributes::DataSource::Hash.build(data) + _, attribute_map = SetAttributes::DataSource::Hash.build(source) test "Hash's keys are used as the mapping" do - assert(hash_source.attribute_map.keys == data.keys) + assert(attribute_map.keys == source.keys) end end end diff --git a/test/automated/data_source/object/get_value.rb b/test/automated/data_source/object/get_value.rb index d8f0853..82d2f7c 100644 --- a/test/automated/data_source/object/get_value.rb +++ b/test/automated/data_source/object/get_value.rb @@ -3,32 +3,16 @@ context "Data Source" do context "Object" do context "Get Value" do - context "Source Attribute Name and Receiver Attribute Name Are the Same" do - mapping = Controls::Mapping::Unbalanced.example + mapping = Controls::Mapping.example - data = SetAttributes::Controls::Object.example + source = SetAttributes::Controls::Object.example - object_source = SetAttributes::DataSource::Object.build(data, mapping) + object_source, _ = SetAttributes::DataSource::Object.build(source, mapping) - value = object_source.get_value(:some_attribute) + value = object_source.get_value(:some_attribute) - test "Source value is retrieved by the receiver attribute name" do - assert(value = data.some_attribute) - end - end - - context "Source Attribute Name and Receiver Attribute Name Are Not the Same" do - mapping = Controls::Mapping::Transform.example - - data = SetAttributes::Controls::Object.example - - object_source = SetAttributes::DataSource::Object.build(data, mapping) - - value = object_source.get_value(:some_attribute) - - test "Source value is retrieved by the receiver attribute name" do - assert(value = data.some_other_attribute) - end + test "Source value is retrieved by attribute method" do + assert(value = source.some_attribute) end end end diff --git a/test/automated/data_source/object/get_value_from_attribute_that_isnt_mapped.rb b/test/automated/data_source/object/get_value_from_attribute_that_isnt_mapped.rb index 3d0734e..92bd25f 100644 --- a/test/automated/data_source/object/get_value_from_attribute_that_isnt_mapped.rb +++ b/test/automated/data_source/object/get_value_from_attribute_that_isnt_mapped.rb @@ -3,11 +3,11 @@ context "Data Source" do context "Object" do context "Get Value from Source Attribute that Isn't Mapped" do - mapping = Controls::Mapping::Unbalanced.example + mapping = Controls::Mapping.example - data = SetAttributes::Controls::Object.example + source = SetAttributes::Controls::Object.example - object_source = SetAttributes::DataSource::Object.build(data, mapping) + object_source, _ = SetAttributes::DataSource::Object.build(source, mapping) value = object_source.get_value(Controls::Attribute::Random.example)