Module: Fixie::Model

Defined in:
lib/fixie.rb

Instance Method Summary collapse

Instance Method Details

#fixture(fixture_name) ⇒ Object

This will return an instance of this class loaded from the fixtures matching the name

Parameters:

  • fixture_name (Symbol)

    The name of the fixture

Returns:

  • (Object)

    An instance of this class



152
153
154
155
156
157
158
159
160
# File 'lib/fixie.rb', line 152

def fixture(fixture_name)
  @fixtures ||= {}
  fixture = @fixtures[fixture_name]
  if fixture
    fixture
  else
    @fixtures[fixture_name] = instantiate_from_fixture(Fixie.fixture(fixture_db_name, fixture_table_name, fixture_name))
  end
end

#fixture_db_nameSymbol

This method determines which database is used to load the fixture. The default implementation is to check the class to see if it has a namespace, like ‘Foo::Bar’, and if it does, return :foo. If it does not have a namespace, it will return :default.

You should override this method if you have multiple databases in your app and you have a different way of determining the DB name based on the class.

Returns:

  • (Symbol)

    The db name for this class



180
181
182
183
184
185
186
# File 'lib/fixie.rb', line 180

def fixture_db_name
  if match_data = name.match(/([^:]+)::/)
    match_data[1].to_sym
  else
    :default
  end
end

#fixture_table_nameSymbol

This method returns the name of the table that the fixtures for this model should be loaded into/from. The default is to just underscore and pluralize the table name, e.g. City => cities.

Returns:

  • (Symbol)

    The table name for this class



193
194
195
196
197
198
199
# File 'lib/fixie.rb', line 193

def fixture_table_name
  @fixture_table_name ||= if respond_to?(:table_name)
    table_name.to_sym
  else
    name.demodulize.tableize.to_sym
  end
end

#instantiate_from_fixture(fixture) ⇒ Object

This method is used to get an instance of the model from a fixture hash. The default implementation is to just pass the hash to the model’s constructor.

Parameters:

  • fixture (Hash<String, Object>)

    The fixture

Returns:

  • (Object)

    An instance of this class



167
168
169
# File 'lib/fixie.rb', line 167

def instantiate_from_fixture(fixture)
  new(fixture)
end