Sha256: 0397836eeb366f1fbf3e596a37d41004b7bcbe00c0b1ca324d2662ee18640fb6

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

require 'moosex'

class TriggerTest
	include MooseX

	has logger: {
		is: :ro
	}

	has attr_with_trigger: {
		is: :rw,
		trigger: :my_method,
	}

	has attr_with_trigger_ro: {
		is: :ro,
		trigger: :my_method,
	}

	has attr_with_default: {
		is: :rw,
		trigger: lambda do |object, new_value| 
			object.logger.log "will update attr_with_trigger with new value #{new_value}"
		end,
		default: 1,
	}

	has attr_lazy_trigger: {
		is: :lazy,
		trigger: :my_method,
		builder: lambda{ |x| 1},
	}

	def my_method(new_value)
		logger.log "will update attr_with_trigger with new value #{new_value}"
	end
end

describe "TriggerTest" do
	it "should call trigger on constructor" do
		log = double
		log.should_receive(:log)
		t = TriggerTest.new(attr_with_trigger: 1, logger: log)

	end

	it "should call trigger on constructor (ro)" do
		log = double
		log.should_receive(:log)
		t = TriggerTest.new(attr_with_trigger_ro: 1, logger: log)

	end

	it "should NOT call trigger on constructor (with default)" do
		log = double
		log.should_not_receive(:log)
		t = TriggerTest.new(logger: log)
	end

	it "should NOT call trigger on constructor (with default)" do
		log = double
		log.should_receive(:log)
		t = TriggerTest.new(logger: log)

		t.attr_with_default = 1
	end

	it "should call trigger on setter" do
		log = double
		log.should_receive(:log)
		t = TriggerTest.new(logger: log)
		
		t.attr_with_trigger = 1
	end

	it "should call trigger on setter" do
		log = double
		log.should_receive(:log)
		t = TriggerTest.new(logger: log)
		
		t.attr_lazy_trigger.should == 1
	end	
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
moosex-0.0.16 spec/trigger_spec.rb
moosex-0.0.15 spec/trigger_spec.rb
moosex-0.0.14 spec/trigger_spec.rb
moosex-0.0.13 spec/trigger_spec.rb
moosex-0.0.12 spec/trigger_spec.rb
moosex-0.0.11 spec/trigger_spec.rb
moosex-0.0.10 spec/trigger_spec.rb