Module: FixturizeMixin
- Included in:
- Class
- Defined in:
- lib/can_has_fixtures/fixturize_mixin.rb
Class Method Summary collapse
Instance Method Summary collapse
- #fixturize!(klass = self) ⇒ Object
- #fixturize_description_args(*args, &block) ⇒ Object
- #method_missing_with_fixture_check(meth, *args, &block) ⇒ Object
Class Method Details
.included(base) ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/can_has_fixtures/fixturize_mixin.rb', line 2 def self.included(base) unless method_defined? :method_missing def method_missing(meth, *args, &block); super; end end base.class_eval do alias_method :method_missing_without_fixture_check, :method_missing alias_method :method_missing, :method_missing_with_fixture_check end end |
Instance Method Details
#fixturize!(klass = self) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 |
# File 'lib/can_has_fixtures/fixturize_mixin.rb', line 36 def fixturize!(klass = self) klass.cattr_accessor :fixture_procs klass.fixture_procs = {} class << klass def fixture(identifier = :default, &block) case identifier when Class then add_fixture(identifier.to_s.snake_case.to_sym, &block) when String then add_fixture(identifier.snake_case.to_sym, &block) when Symbol then add_fixture(identifier, &block) end end def add_fixture(sym, &block) fixture_procs[sym] = fixture_procs[sym] ? fixture_procs[sym] << block : [block] end def generate(*args, &block) args, attributes = (*args) identifier = args.first || :default attr_procs = fixture_procs[identifier.to_s.snake_case.to_sym] raise "#{identifier} fixture was not found" if attr_procs.nil? || attr_procs.empty? proc = attr_procs.random values = proc.call(*args) case values when Hash then create(values.merge(attributes)) else raise "#{identifier.to_s.camel_case} fixture's attributes were not recognized" end end alias_method :gen, :generate end end |
#fixturize_description_args(*args, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/can_has_fixtures/fixturize_mixin.rb', line 26 def fixturize_description_args(*args, &block) klass = description_args.first case klass when Class then fixturize!(klass) unless klass.respond_to?(:fixture) end klass.fixture(args.first || :default, &block) end |
#method_missing_with_fixture_check(meth, *args, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/can_has_fixtures/fixturize_mixin.rb', line 13 def method_missing_with_fixture_check(meth, *args, &block) if meth.to_s == "fixture" if respond_to?(:description_args) fixturize_description_args(*args, &block) else fixturize! fixture(*args, &block) end else method_missing_without_fixture_check(meth, *args, &block) end end |