aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support/data_modifier.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/data_modifier.rb')
-rw-r--r--spec/support/data_modifier.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/spec/support/data_modifier.rb b/spec/support/data_modifier.rb
new file mode 100644
index 000000000..2b3b00ac5
--- /dev/null
+++ b/spec/support/data_modifier.rb
@@ -0,0 +1,53 @@
+require_relative 'data_modifier/enum'
+require_relative 'data_modifier/hash'
+module Support
+ module DataModifier
+ module InstanceMethods
+ CannotModify = Class.new RuntimeError
+
+ def advance_values(atts, *keys)
+ keys.inject(atts){ |h, k| h.merge( k => atts[k].next) }
+ end
+
+ # return array of atts wich each value modified, unboxing
+ # values if needed
+ def modify_atts(base_atts)
+ base_atts.keys.map do | key |
+ modify_att base_atts, key
+ end.compact
+ end
+
+ private
+ def modify_att atts, key
+ atts.merge(key => modify_value(atts[key]))
+ rescue CannotModify
+ nil
+ end
+ def modify_value value
+ case value
+ when String
+ "#{value}."
+ when Fixnum
+ value + 1
+ when TrueClass
+ false
+ when FalseClass
+ true
+ when Float
+ value * 1.1
+ when Date
+ value + 1.day
+ when Box
+ value.next.value
+ else
+ raise CannotModify
+ end
+ end
+ end
+ end
+end
+
+RSpec.configure do | c |
+ c.include Support::DataModifier::InstanceMethods, type: :checksum
+ c.include Support::DataModifier::InstanceMethods, type: :model
+end