Module: ShouldaCreate
- Defined in:
- lib/shoulda_create/shoulda_create.rb
Instance Method Summary collapse
- #get_options!(args, *wanted) ⇒ Object
- #should_change(description, options = {}, &block) ⇒ Object
-
#should_change_record_count_of(class_name, amount, action) ⇒ Object
:nodoc:.
- #should_create(class_name, options = {}) ⇒ Object
- #should_destroy(class_name, options = {}) ⇒ Object
- #should_not_change(description, &block) ⇒ Object
-
#should_not_change_record_count_of(class_name) ⇒ Object
:nodoc:.
- #should_not_create(class_name) ⇒ Object
- #should_not_destroy(class_name) ⇒ Object
Instance Method Details
#get_options!(args, *wanted) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/shoulda_create/shoulda_create.rb', line 97 def (args, *wanted) ret = [] opts = (args.last.is_a?(Hash) ? args.pop : {}) wanted.each {|w| ret << opts.delete(w)} wanted.each {|w| ret << opts.delete(w) } raise ArgumentError, "Unsupported options given: #{opts.keys.join(', ')}" unless opts.keys.empty? return *ret if ret.compact.empty? return nil else return *ret end end |
#should_change(description, options = {}, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/shoulda_create/shoulda_create.rb', line 50 def should_change(description, = {}, &block) by, from, to = ([], :by, :from, :to) stmt = "change #{description}" stmt << " from #{from.inspect}" if from stmt << " to #{to.inspect}" if to && !to.is_a?(Proc) stmt << " by #{by.inspect}" if by if block_given? code = block else warn "[DEPRECATION] should_change(expression, options) is deprecated. " << "Use should_change(description, options) { code } instead." code = lambda { eval(description) } end before_var_name = '@_before_should_change_' + description.downcase.gsub(/[^\w]/, '_') before = lambda { self.instance_variable_set( before_var_name, code.bind(self).call ) } should stmt, :before => before do old_value = self.instance_variable_get( before_var_name ) new_value = code.bind(self).call to = to.bind(self).call if to.is_a?(Proc) assert_operator from, :===, old_value, "#{description} did not originally match #{from.inspect}" if from assert_not_equal old_value, new_value, "#{description} did not change" unless by == 0 assert_operator to, :===, new_value, "#{description} was not changed to match #{to.inspect}" if to assert_equal old_value + by, new_value if by end end |
#should_change_record_count_of(class_name, amount, action) ⇒ Object
:nodoc:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/shoulda_create/shoulda_create.rb', line 2 def should_change_record_count_of(class_name, amount, action) # :nodoc: klass = class_name.is_a?(Symbol) ? class_name.to_s.camelize.constantize : class_name var_name = "@_before_change_record_count_#{class_name.to_s.gsub(/:+/, '_')}" counter = lambda do if class_name.is_a?(String) ActiveRecord::Base.connection.select_one("SELECT count(*) FROM #{class_name}")["count"].to_i else klass.count end end before = lambda do instance_variable_set var_name, counter.call end human_name = class_name.to_s.humanize.downcase count = 'a' if amount > 1 human_name = human_name.pluralize count = amount end should "#{action} #{count} #{human_name}", :before => before do assert_equal instance_variable_get(var_name) + amount, counter.call, "Expected to #{action} a #{human_name}" end end |
#should_create(class_name, options = {}) ⇒ Object
40 41 42 43 |
# File 'lib/shoulda_create/shoulda_create.rb', line 40 def should_create(class_name, = {}) count = [:count] || 1 should_change_record_count_of(class_name, count, 'create') end |
#should_destroy(class_name, options = {}) ⇒ Object
45 46 47 48 |
# File 'lib/shoulda_create/shoulda_create.rb', line 45 def should_destroy(class_name, = {}) count = [:count] || -1 should_change_record_count_of(class_name, count, 'destroy') end |
#should_not_change(description, &block) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/shoulda_create/shoulda_create.rb', line 78 def should_not_change(description, &block) if block_given? code = block else warn "[DEPRECATION] should_change(expression, options) is deprecated. " << "Use should_change(description, options) { code } instead." code = lambda { eval(description) } end before_var_name = '@_before_should_change_' + description.downcase.gsub(/[^\w]/, '_') before = lambda { self.instance_variable_set( before_var_name, code.bind(self).call ) } should "not change #{description}", :before => before do old_value = self.instance_variable_get( before_var_name ) new_value = code.bind(self).call assert_equal old_value, new_value, "#{description} changed" end end |
#should_not_change_record_count_of(class_name) ⇒ Object
:nodoc:
28 29 30 |
# File 'lib/shoulda_create/shoulda_create.rb', line 28 def should_not_change_record_count_of(class_name) # :nodoc: should_change_record_count_of class_name, 0, 'neither create nor destroy' end |
#should_not_create(class_name) ⇒ Object
32 33 34 |
# File 'lib/shoulda_create/shoulda_create.rb', line 32 def should_not_create class_name should_change_record_count_of(class_name, 0, 'not create') end |
#should_not_destroy(class_name) ⇒ Object
36 37 38 |
# File 'lib/shoulda_create/shoulda_create.rb', line 36 def should_not_destroy class_name should_change_record_count_of(class_name, 0, 'not destroy') end |