Class: DataMapper::Sweatshop

Inherits:
Object
  • Object
show all
Extended by:
Unique
Defined in:
lib/dm-sweatshop/unique.rb,
lib/dm-sweatshop/sweatshop.rb,
lib/dm-sweatshop/support/class_attributes.rb

Defined Under Namespace

Modules: ClassAttributes, Unique Classes: NoFixtureExist, UniqueWorker

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Unique

unique

Class Attribute Details

.model_mapObject

Returns the value of attribute model_map.



12
13
14
# File 'lib/dm-sweatshop/sweatshop.rb', line 12

def model_map
  @model_map
end

.record_mapObject

Returns the value of attribute record_map.



13
14
15
# File 'lib/dm-sweatshop/sweatshop.rb', line 13

def record_map
  @record_map
end

Class Method Details

.add(klass, name, &proc) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a Proc to model map. Proc must return a Hash of attributes.

Parameters:

  • klass (Class, DataMapper::Resource)
  • name (Symbol)
  • instance (DataMapper::Resource)

Returns:

  • (Array)

    model map



32
33
34
# File 'lib/dm-sweatshop/sweatshop.rb', line 32

def self.add(klass, name, &proc)
  self.model_map[klass][name.to_sym] << proc
end

.attributes(klass, name) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a Hash of attributes from the model map

Parameters:

  • klass (Class, DataMapper::Resource)
  • name (Symbol)

Returns:

  • (Hash)

    existing instance of a model from the model map

Raises:

  • NoFixtureExist when requested fixture does not exist in the model map



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dm-sweatshop/sweatshop.rb', line 114

def self.attributes(klass, name)
  proc = model_map[klass][name.to_sym].pick

  if proc
    expand_callable_values(proc.call)
  elsif klass.superclass.is_a?(DataMapper::Model)
    attributes(klass.superclass, name)
  else
    raise NoFixtureExist, "#{name} fixture was not found for class #{klass}"
  end
end

.create(klass, name, attributes = {}) ⇒ DataMapper::Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates an instance from given hash of attributes, saves it and adds it to the record map.

Parameters:

  • klass (Class, DataMapper::Resource)
  • name (Symbol)
  • attributes (Hash) (defaults to: {})

Returns:

  • (DataMapper::Resource)

    added instance



74
75
76
# File 'lib/dm-sweatshop/sweatshop.rb', line 74

def self.create(klass, name, attributes = {})
  record(klass, name, klass.create(attributes(klass, name).merge(attributes)))
end

.create!(klass, name, attributes = {}) ⇒ DataMapper::Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Same as create but calls Model#create! and does save invalid models

Parameters:

  • klass (Class, DataMapper::Resource)
  • name (Symbol)
  • attributes (Hash) (defaults to: {})

Returns:

  • (DataMapper::Resource)

    added instance



60
61
62
# File 'lib/dm-sweatshop/sweatshop.rb', line 60

def self.create!(klass, name, attributes = {})
  record(klass, name, klass.create!(attributes(klass, name).merge(attributes)))
end

.expand_callable_values(hash) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a Hash with callable values evaluated.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)

    existing instance of a model from the model map



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dm-sweatshop/sweatshop.rb', line 133

def self.expand_callable_values(hash)
  expanded = {}
  hash.each do |key, value|
    if value.respond_to?(:call)
      expanded[key] = value.call
    else
      expanded[key] = value
    end
  end
  expanded
end

.make(klass, name, attributes = {}) ⇒ DataMapper::Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates an instance from given hash of attributes and adds it to records map without saving.

Parameters:

  • klass (Class, DataMapper::Resource)
  • name (Symbol)
  • attributes (Hash) (defaults to: {})

Returns:

  • (DataMapper::Resource)

    added instance



88
89
90
# File 'lib/dm-sweatshop/sweatshop.rb', line 88

def self.make(klass, name, attributes = {})
  record(klass, name, klass.new(attributes(klass, name).merge(attributes)))
end

.pick(klass, name) ⇒ DataMapper::Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a pre existing instance of a model from the record map

Parameters:

  • klass (Class, DataMapper::Resource)
  • name (Symbol)

Returns:

  • (DataMapper::Resource)

    existing instance of a model from the record map

Raises:

  • DataMapper::Sweatshop::NoFixtureExist when requested fixture does not exist in the record map



101
102
103
# File 'lib/dm-sweatshop/sweatshop.rb', line 101

def self.pick(klass, name)
  self.record_map[klass][name.to_sym].pick || raise(NoFixtureExist, "no #{name} context fixtures have been generated for the #{klass} class")
end

.record(klass, name, instance) ⇒ DataMapper::Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds an instance to records map.

Parameters:

  • klass (Class, DataMapper::Resource)
  • name (Symbol)
  • instance (DataMapper::Resource)

Returns:

  • (DataMapper::Resource)

    added instance



45
46
47
48
# File 'lib/dm-sweatshop/sweatshop.rb', line 45

def self.record(klass, name, instance)
  self.record_map[klass][name.to_sym] << instance
  instance
end