Class: DNApi::Sweatshop

Inherits:
Object
  • Object
show all
Defined in:
lib/dnapi/test/sweatshop.rb

Defined Under Namespace

Classes: NoFixtureExist

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.model_mapObject

Returns the value of attribute model_map.



15
16
17
# File 'lib/dnapi/test/sweatshop.rb', line 15

def model_map
  @model_map
end

.record_mapObject

Returns the value of attribute record_map.



16
17
18
# File 'lib/dnapi/test/sweatshop.rb', line 16

def record_map
  @record_map
end

Class Method Details

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

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)


35
36
37
# File 'lib/dnapi/test/sweatshop.rb', line 35

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

.attributes(klass, name) ⇒ Object

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)


117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dnapi/test/sweatshop.rb', line 117

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?(Struct)
    attributes(klass.superclass, name)
  else
    raise NoFixtureExist, "#{name} fixture was not found for class #{klass}"
  end
end

.create(klass, name, attributes = {}) ⇒ Object

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: {})


77
78
79
# File 'lib/dnapi/test/sweatshop.rb', line 77

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

.create!(klass, name, attributes = {}) ⇒ Object

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: {})


63
64
65
# File 'lib/dnapi/test/sweatshop.rb', line 63

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

.expand_callable_values(hash) ⇒ Object

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)


136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dnapi/test/sweatshop.rb', line 136

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 = {}) ⇒ Object

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: {})


91
92
93
# File 'lib/dnapi/test/sweatshop.rb', line 91

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

.pick(klass, name) ⇒ Object

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)


104
105
106
# File 'lib/dnapi/test/sweatshop.rb', line 104

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) ⇒ Object

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)


48
49
50
51
# File 'lib/dnapi/test/sweatshop.rb', line 48

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