Module: FluentFixtures::DSL

Defined in:
lib/fluent_fixtures/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_fixtureObject

The name of the base fixture for the fixture module as a Symbol



23
24
25
# File 'lib/fluent_fixtures/dsl.rb', line 23

def base_fixture
  @base_fixture
end

#collectionObject

The FluentFixtures::Collection the fixture is part of



27
28
29
# File 'lib/fluent_fixtures/dsl.rb', line 27

def collection
  @collection
end

#decorator_optionsObject (readonly)

The Hash of options hashes for declared decorators



19
20
21
# File 'lib/fluent_fixtures/dsl.rb', line 19

def decorator_options
  @decorator_options
end

#decoratorsObject (readonly)

The Hash of decorators declared for this fixture module



15
16
17
# File 'lib/fluent_fixtures/dsl.rb', line 15

def decorators
  @decorators
end

Instance Method Details

#additions_for(other_fixture, depends_on: nil, &block) ⇒ Object

Declare decorators for the other_fixture instead of the current one.



101
102
103
104
105
106
107
108
# File 'lib/fluent_fixtures/dsl.rb', line 101

def additions_for( other_fixture, depends_on: nil, &block )
	self.depends_on( depends_on ) if depends_on

	mod = self.collection.modules[ other_fixture ] or
		raise "no such fixture %p" % [ other_fixture ]

	mod.module_eval( &block )
end

#after_saving(&block) ⇒ Object

Add a callback to the fixture that will be passed new instances after it’s saved. The results of the block will be used as the fixtured instance.



138
139
140
# File 'lib/fluent_fixtures/dsl.rb', line 138

def after_saving( &block )
	define_singleton_method( :call_after_saving, &block )
end

#alias_decorator(new_name, original_name) ⇒ Object

Declare a new_name for the decorator declared with with original_name.



119
120
121
122
123
124
125
# File 'lib/fluent_fixtures/dsl.rb', line 119

def alias_decorator( new_name, original_name )
	block = self.decorators[ original_name.to_sym ] or
		raise ScriptError, "undefined decorator %p" % [ original_name ]
	self.decorators[ new_name.to_sym ] = block
	self.decorator_options[ new_name.to_sym ] =
		self.decorator_options[ original_name.to_sym ].dup
end

#base(name = nil, &block) ⇒ Object

Declare a base fixture for the current module called name, with an optional initial decorator as a block. If no name is given, one is chosen based on the name of the declaring module.



40
41
42
43
44
45
46
47
# File 'lib/fluent_fixtures/dsl.rb', line 40

def base( name=nil, &block )
	name ||= self.collection.default_base_fixture_name( self )

	self.base_fixture = name
	self.decorators[ :_ ] = block if block

	self.collection.add_base_fixture( name, self )
end

#before_saving(&block) ⇒ Object

Add a callback to the fixture that will be passed new instances after all decorators have been applied and immediately before it’s saved. The results of the block will be used as the fixtured instance.



131
132
133
# File 'lib/fluent_fixtures/dsl.rb', line 131

def before_saving( &block )
	define_singleton_method( :call_before_saving, &block )
end

#compose(**hash, &block) ⇒ Object

Declare a decorator that is composed out of other decorators and an optional block. The first hash pair should be the name of the declared decorator and the names of the decorator/s it is composed of.

Example:

decorator :foo { ... }
decorator :bar { ... }
compose( :simple => :foo ) { ... }
compose( :complex => [:foo, :bar] ) { ... }
compose( :complex_with_args => {foo: [1,2], bar: "Something"} ) { ... }

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fluent_fixtures/dsl.rb', line 84

def compose( **hash, &block )
	name, components = hash.first

	raise ArgumentError, "expected a name and one or more component decorators" unless name
	unless [Symbol, Array, Hash].include?( components.class )
		raise ArgumentError, "invalid compose values: expected symbol, array, or hash; got %p" %
			[ components.class ]
	end

	options = hash.reject {|k,_| k == name }.merge( prelude: components )
	block ||= Proc.new {}

	self.decorator( name, **options, &block )
end

#decorator(name, **options, &block) ⇒ Object

Declare a decorator for the fixture with the specified name that will use the given block.



66
67
68
69
70
# File 'lib/fluent_fixtures/dsl.rb', line 66

def decorator( name, **options, &block )
	name = name.to_sym
	self.decorators[ name ] = block
	self.decorator_options[ name ] = options
end

#decorator?(name) ⇒ Boolean Also known as: has_decorator?

Returns true if there is a decorator with the specified name.

Returns:

  • (Boolean)


112
113
114
# File 'lib/fluent_fixtures/dsl.rb', line 112

def decorator?( name )
	return self.decorators.key?( name.to_sym )
end

#depends_on(*other_fixtures) ⇒ Object

Register one or more other_fixtures that should be loaded when this fixture is loaded.



59
60
61
# File 'lib/fluent_fixtures/dsl.rb', line 59

def depends_on( *other_fixtures )
	self.collection.load( *other_fixtures )
end

#factory(*args, &block) ⇒ Object

Return an instance of Cozy::FluentFixtures::FluentFactory for the base fixture of the receiving module.



145
146
147
# File 'lib/fluent_fixtures/dsl.rb', line 145

def factory( *args, &block )
	return FluentFixtures::Factory.new( self, *args, &block )
end

#fixtured_class(new_class = nil) ⇒ Object

Get/set the Class the fixture will use.



51
52
53
54
# File 'lib/fluent_fixtures/dsl.rb', line 51

def fixtured_class( new_class=nil )
	@fixtured_class = new_class if new_class
	return @fixtured_class
end

#fixtured_instance(*args, &block) ⇒ Object

Return an unsaved instance of the fixtured class with the specified args and block, applying the base decorator if there is one.



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fluent_fixtures/dsl.rb', line 152

def fixtured_instance( *args, &block )
	fixclass = self.fixtured_class or
		raise ScriptError, "%p doesn't declare its fixtured class!" % [ self ]

	instance = fixclass.new( *args, &block )

	if (( base_decorator = self.decorators[:_] ))
		self.log.debug "Applying base decorator to %p" % [ instance ]
		instance.instance_exec( &base_decorator )
	end

	return instance
end