Class: Fixtures

Inherits:
Object
  • Object
show all
Defined in:
lib/joinfix/fixtures.rb,
lib/joinfix/fixtures_class.rb

Overview

Fixtures objects are essentially nested hashes:

# A Fixtures is a hash of entry name keys and Fixture values
entry_name:
  # A Fixture is a hash of entry fields and values
  field: value

Fixtures also tracks data required for loading entries into the database, such as table_name and the connection to use. See ‘active_record/fixtures.rb’ for more details.

JoinFix extends Fixtures to track additional information like the ActiveRecord class modeling the entry data. Using reflection, Fixtures can figure out which fields in an entry correspond to an association. Entries with association fields are

extracted and turned into join entries. Consider and ActiveRecord class User, with ‘name’ as an attribute and a ‘groups’ association…

[users.yml]
 # This entry will not be extracted as a template because it defines no 'groups'
 john:
   name: John                     

 # This entry will be extracted because it defines the 'groups' association
 jane:
   name: Jane
   groups:                   
     - admin_group
     - workers

If you loaded users.yml into a Fixtures…

users = Fixtures.new( ...arguments to load users.yml...)
users.to_hash     # => {'john' => ..., 'jane' => ...}
users.templates   # => nil

users.extract_templates
users.to_hash     # => {'john' => ...}
users.templates   # => {'jane' => ...}

The entry templates are extended with JoinFix and used to create the join entries.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Fixtures

:nodoc:



44
45
46
47
48
49
# File 'lib/joinfix/fixtures.rb', line 44

def initialize(*args) # :nodoc:
	join_fix_original_initialize(*args)
	@klass = Object.const_get(@class_name)
   @association_names = klass.reflect_on_all_associations.collect {|assoc| assoc.name.to_s}
	@join_configs = {}
end

Instance Attribute Details

#association_namesObject (readonly)

Returns the value of attribute association_names.



41
42
43
# File 'lib/joinfix/fixtures.rb', line 41

def association_names
  @association_names
end

#fixture_pathObject (readonly)

Returns the value of attribute fixture_path.



41
42
43
# File 'lib/joinfix/fixtures.rb', line 41

def fixture_path
  @fixture_path
end

#klassObject (readonly)

Returns the value of attribute klass.



41
42
43
# File 'lib/joinfix/fixtures.rb', line 41

def klass
  @klass
end

#templatesObject (readonly)

Returns the value of attribute templates.



41
42
43
# File 'lib/joinfix/fixtures.rb', line 41

def templates
  @templates
end

Class Method Details

.fixture(table_name) ⇒ Object

Retreives the Fixtures for the given table name. Raises an error if this Fixtures has not yet been loaded.



43
44
45
46
47
# File 'lib/joinfix/fixtures_class.rb', line 43

def fixture(table_name)
	fixture = all_loaded_fixtures[table_name.to_s]
	raise MissingFixtureError.new(table_name) unless fixture
	fixture
end

.make_join_fixturesObject

fixtures, after all fixtures have been loaded.

After make_join_fixtures completes, entries for tables matching ENV are printed to STDOUT. You can print entries for each loaded fixture by setting ENV = ‘true’. By default, no entries are printed.

Note: printing does not affect the creation or loading of fixtures.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/joinfix/fixtures_class.rb', line 12

def make_join_fixtures
	all_loaded_fixtures.each_pair do |table_name, fixtures|
		fixtures.make_child_entries
	end
    
	# note indexing and reference resolution must execute after all 
	# fixtures have been templated because you never know which fixture(s) 
	# will recieve new entries.  Additionally, indexing and resolution must
	# run separately, because reference resolution requires the primary key
    # be set for the referenced entry.
	all_loaded_fixtures.values.each {|fixtures| index_fixtures(fixtures) }
	all_loaded_fixtures.values.each {|fixtures| resolve_references(fixtures)}
    
    # Print entries to STDOUT for review, if applicable
    joinfix_dump = ENV['joinfix_dump']
    if joinfix_dump
      print_all = (joinfix_dump.to_s =~ /^true$/i)
      regexp = Regexp.new(joinfix_dump.to_s)
      
      all_loaded_fixtures.values.each  do |fixtures|
        table_name = fixtures.klass.table_name
        next unless print_all || table_name =~ regexp
        
        puts "------- #{fixtures.klass.table_name} --------"
        puts fixtures.to_hash.to_yaml.gsub(/^---/, '') + "\n"
      end
    end
end

Instance Method Details

#each_pair(&block) ⇒ Object

Iterates through each entry



79
80
81
# File 'lib/joinfix/fixtures.rb', line 79

def each_pair(&block) 
	map { |entry_name, fixture| yield(entry_name, fixture) } 
end

#insert_fixturesObject

Extends the default method to provide a hook for making join fixtures.

Now insert_fixtures calls Fixtures.make_join_fixtures before calling the original insert_fixtures method.



56
57
58
59
# File 'lib/joinfix/fixtures.rb', line 56

def insert_fixtures
	Fixtures.make_join_fixtures if templates.nil?
	join_fix_original_insert_fixtures
end

#join_config(assoc_name) ⇒ Object

Returns the join configuration (as per JoinFix.configure) for the given association.



74
75
76
# File 'lib/joinfix/fixtures.rb', line 74

def join_config(assoc_name)
	@join_configs[assoc_name] ||= JoinFix.send("configure", klass, assoc_name)
end

#join_fix_original_initializeObject



43
# File 'lib/joinfix/fixtures.rb', line 43

alias join_fix_original_initialize initialize

#join_fix_original_insert_fixturesObject



51
# File 'lib/joinfix/fixtures.rb', line 51

alias join_fix_original_insert_fixtures insert_fixtures

#make_child_entriesObject

Extracts the child entry templates then makes the entries specified by each template. Does not execute if templates have already been extracted.



64
65
66
67
68
69
70
71
# File 'lib/joinfix/fixtures.rb', line 64

def make_child_entries
	return unless templates.nil?
	
	extract_templates
	templates.each_pair do |entry_name, entry|
		make_entry(entry_name, entry, true)
	end
end

#primary_key_offsetObject

The initial offset for indexing (ie 0 so that indexing starts at 1).

This method is provided as a hook for overrides if necessary.



94
95
96
# File 'lib/joinfix/fixtures.rb', line 94

def primary_key_offset
  0
end

#to_hashObject

Returns all entries as a hash of => entry pairs



84
85
86
87
88
89
90
# File 'lib/joinfix/fixtures.rb', line 84

def to_hash
	hash = {}
	each_pair do |entry_name, entry|
	     hash[entry_name] = entry.to_hash
	end
	hash
end