Skip to content

Commit

Permalink
Merge branch 'remediate-data-store-design'
Browse files Browse the repository at this point in the history
  • Loading branch information
sbellware committed May 22, 2019
2 parents 8292164 + 29e7686 commit 56d06c1
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 55 deletions.
30 changes: 6 additions & 24 deletions lib/set_attributes/data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,27 @@ module DataSource
def self.included(cls)
cls.class_exec do
extend Build

def self.assure_include(source, include)
include
end
end
end

attr_reader :source
attr_reader :attribute_map

def initialize(source)
@source = source
end

module Build
def build(source, include=nil, exclude: nil)
include = assure_include(source, include)

include ||= []
include = Array(include)

exclude ||= []
exclude = Array(exclude)

attribute_map = SetAttributes::Map.build(include)
attribute_map.exclude(exclude)

instance = new(source)

return instance, attribute_map
def build(source)
new(source)
end
end

def self.build_data_source(source, include=nil, exclude: nil)
data_source_class = select_class(source)
data_source_class.build(source, include, exclude: exclude)
def self.build_data_source(source)
data_source_class = implementation(source)
data_source_class.build(source)
end

def self.select_class(source)
def self.implementation(source)
if source.is_a?(::Hash)
return DataSource::Hash
else
Expand Down
4 changes: 2 additions & 2 deletions lib/set_attributes/data_source/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module DataSource
class Hash
include DataSource

def self.assure_include(source, include)
if include.nil? || include.empty?
def self.verify_mapping(source, include)
if include.nil?
return source.keys
end

Expand Down
2 changes: 1 addition & 1 deletion lib/set_attributes/data_source/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Object

Error = Class.new(RuntimeError)

def self.assure_include(source, include)
def self.verify_mapping(source, include)
if include.nil?
raise Error, "Object source is missing the include mapping"
end
Expand Down
10 changes: 9 additions & 1 deletion lib/set_attributes/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ def mappings
@mappings ||= []
end

def self.build(mappings)
def self.build(mappings, exclude: nil)
mappings ||= []
mappings = Array(mappings)

exclude ||= []
exclude = Array(exclude)

instance = new

mappings.each do |mapping|
instance.add(mapping)
end

instance.exclude(exclude)

instance
end

Expand Down
7 changes: 6 additions & 1 deletion lib/set_attributes/set_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ def self.build(receiver, source, copy: nil, include: nil, exclude: nil, strict:
include = copy
end

data_source, attribute_map = SetAttributes::DataSource.build_data_source(source, include, exclude: exclude)
data_source_implementation = DataSource.implementation(source)
include = data_source_implementation.verify_mapping(source, include)

attribute_map = SetAttributes::Map.build(include, exclude: exclude)

data_source = data_source_implementation.build(source)

new(receiver, data_source, attribute_map).tap do |instance|
instance.strict = strict
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require_relative '../../test_init'

context "Data Source" do
context "Attribute Map" do
context "Excluded from Build" do
mapping = Controls::Mapping.example

context "Exclude One Mapping" do
_, attribute_map = SetAttributes::Controls::DataSource.example(exclude: [:some_other_attribute])
attribute_map = SetAttributes::Map.build(mapping, exclude: [:some_other_attribute])

test "Mapped attributes are included" do
assert(attribute_map.include?(:some_attribute))
Expand All @@ -15,7 +17,7 @@
end

context "Exclude Many Mappings" do
_, attribute_map = SetAttributes::Controls::DataSource.example(exclude: [:some_attribute, :some_other_attribute])
attribute_map = SetAttributes::Map.build(mapping, exclude: [:some_attribute, :some_other_attribute])

test "Excluded mappings are removed from the attribute map" do
refute(attribute_map.include?(:some_attribute))
Expand Down
2 changes: 1 addition & 1 deletion test/automated/data_source/factory/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 1 addition & 3 deletions test/automated/data_source/factory/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
context "Object" do
source = Controls::Object.example

placeholder_mapping = []

data_source, _ = SetAttributes::DataSource.build_data_source(source, placeholder_mapping)
data_source = SetAttributes::DataSource.build_data_source(source)

test "Is an object data source" do
assert(data_source.is_a?(SetAttributes::DataSource::Object))
Expand Down
4 changes: 1 addition & 3 deletions test/automated/data_source/hash/get_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
context "Data Source" do
context "Hash" do
context "Get Value" do
mapping = Controls::Mapping.example

source = SetAttributes::Controls::Hash.example

hash_source, _ = SetAttributes::DataSource::Hash.build(source, mapping)
hash_source = SetAttributes::DataSource::Hash.build(source)

value = hash_source.get_value(:some_attribute)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
context "Data Source" do
context "Hash" do
context "Get Value from Source Attribute that Isn't Mapped" do
mapping = nil

source = SetAttributes::Controls::Hash.example

hash_source, _ = SetAttributes::DataSource::Hash.build(source, mapping)
hash_source = SetAttributes::DataSource::Hash.build(source)

value = hash_source.get_value(Controls::Attribute::Random.example)

Expand Down
4 changes: 1 addition & 3 deletions test/automated/data_source/object/get_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
context "Data Source" do
context "Object" do
context "Get Value" do
mapping = Controls::Mapping.example

source = SetAttributes::Controls::Object.example

object_source, _ = SetAttributes::DataSource::Object.build(source, mapping)
object_source = SetAttributes::DataSource::Object.build(source)

value = object_source.get_value(:some_attribute)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
context "Data Source" do
context "Object" do
context "Get Value from Source Attribute that Isn't Mapped" do
mapping = Controls::Mapping.example

source = SetAttributes::Controls::Object.example

object_source, _ = SetAttributes::DataSource::Object.build(source, mapping)
object_source = SetAttributes::DataSource::Object.build(source)

value = object_source.get_value(Controls::Attribute::Random.example)

Expand Down
2 changes: 1 addition & 1 deletion test/automated/missing_receiver_attributes/ignored.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
source_type = source_info[1]

context "#{source_type} Source" do
data_source, _ = SetAttributes::DataSource.build_data_source(source, mapping)
data_source = SetAttributes::DataSource.build_data_source(source)

refute(data_source.get_value(:some_attribute).nil?)
refute(data_source.get_value(:some_other_attribute).nil?)
Expand Down
2 changes: 1 addition & 1 deletion test/automated/missing_receiver_attributes/strict.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

context "#{source_type} Source" do

data_source, _ = SetAttributes::DataSource.build_data_source(source, mapping)
data_source = SetAttributes::DataSource.build_data_source(source)

refute(data_source.get_value(:some_attribute).nil?)
refute(data_source.get_value(:some_other_attribute).nil?)
Expand Down
16 changes: 16 additions & 0 deletions test/automated/set_attributes/hash_source/mapping_is_omitted.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require_relative '../../../test_init'

context "Set Attributes" do
context "Object Source" do
context "Mapping is Omitted" do
source = SetAttributes::Controls::Hash.example
reciever = nil

set_attributes = SetAttributes.build(reciever, source)

test "Hash's keys are used as the mapping" do
assert(set_attributes.attribute_map.keys == source.keys)
end
end
end
end
2 changes: 1 addition & 1 deletion test/automated/set_attributes/missing_source_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
source = source_info[0]
source_type = source_info[1]

data_source, _ = SetAttributes::DataSource.build_data_source(source, mapping)
data_source = SetAttributes::DataSource.build_data_source(source)

refute(data_source.get_value(:some_attribute).nil?)
refute(data_source.get_value(:some_other_attribute).nil?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

context "Set Attributes" do
context "Object Source" do
context "Mapping Not Specified" do
context "Mapping is Omitted" do
source = nil
reciever = nil

test "Is an error" do
__ = nil
assert proc { SetAttributes.(__, __) } do
assert proc { SetAttributes.build(reciever, source) } do
raises_error? SetAttributes::DataSource::Object::Error
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/automated/set_attributes/transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
source = source_info[0]
source_type = source_info[1]

data_source, _ = SetAttributes::DataSource.build_data_source(source, mapping)
data_source = SetAttributes::DataSource.build_data_source(source)

refute(data_source.get_value(:an_attribute).nil?)
refute(data_source.get_value(:some_other_attribute).nil?)
Expand Down

0 comments on commit 56d06c1

Please sign in to comment.