Skip to content

Commit

Permalink
Data source attribute access and detection affordances
Browse files Browse the repository at this point in the history
  • Loading branch information
sbellware committed May 22, 2019
1 parent 56d06c1 commit ee6b8ba
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 41 deletions.
9 changes: 7 additions & 2 deletions lib/set_attributes/data_source/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ def self.verify_mapping(source, include)
return include
end

def get_value(source_attribute)
source[source_attribute]
def get_value(attribute)
source[attribute]
end
alias :[] :get_value

def attribute?(attribute)
source.keys.include?(attribute)
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions lib/set_attributes/data_source/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ def self.verify_mapping(source, include)
return include
end

def get_value(source_attribute)
return nil unless source.respond_to?(source_attribute)
source.send(source_attribute)
def get_value(attribute)
return nil unless attribute?(attribute)
source.send(attribute)
end
alias :[] :get_value

def attribute?(attribute)
source.respond_to?(attribute)
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions test/automated/data_source/attribute_predicate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative '../../test_init'

context "Data Source" do
context "Attribute Predicate" do
hash_source = Controls::Hash.example
object_source = Controls::Object.example

[[hash_source, 'Hash'], [object_source, 'Object']].each do |source_info|

source = source_info[0]
source_type = source_info[1]

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

value = data_source.get_value(:some_attribute)

context "Attribute Present in the Source" do
test "Detected" do
assert(data_source.attribute?(:some_attribute))
end
end

context "Attribute Not Present in the Source" do
test "Not Detected" do
refute(data_source.attribute?(Controls::Attribute::Random.example))
end
end
end
end
end
end
15 changes: 0 additions & 15 deletions test/automated/data_source/hash/_include_is_omitted.rb

This file was deleted.

15 changes: 0 additions & 15 deletions test/automated/data_source/object/_include_is_omitted.rb

This file was deleted.

6 changes: 3 additions & 3 deletions test/automated/set_attributes/missing_source_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

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?)
assert(data_source.get_value(:yet_another_attribute).nil?)
refute(data_source[:some_attribute].nil?)
refute(data_source[:some_other_attribute].nil?)
assert(data_source[:yet_another_attribute].nil?)

context "#{source_type} Source" do
receiver = Controls::Object::New.example
Expand Down
6 changes: 3 additions & 3 deletions test/automated/set_attributes/transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

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?)
assert(data_source.get_value(:yet_another_attribute).nil?)
refute(data_source[:an_attribute].nil?)
refute(data_source[:some_other_attribute].nil?)
assert(data_source[:yet_another_attribute].nil?)

context "#{source_type} Source" do
receiver = Controls::Object::New.example
Expand Down

0 comments on commit ee6b8ba

Please sign in to comment.