Class: Punchbowl::Fastory

Inherits:
Object
  • Object
show all
Defined in:
lib/fastory.rb

Constant Summary collapse

@@fastories =
{}
@@sql_inserts =
[]
@@record_sql =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Fastory

Returns a new instance of Fastory.



32
33
34
35
# File 'lib/fastory.rb', line 32

def initialize(name, options = {})
  self.name = name
  self.options = options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/fastory.rb', line 25

def name
  @name
end

#optionsObject

Returns the value of attribute options.



25
26
27
# File 'lib/fastory.rb', line 25

def options
  @options
end

Instance Method Details

#process!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fastory.rb', line 37

def process! 
  options = transform_associations

  if template_available?
    cache = @@fastories[cache_key]
    cnt = cache[:templates_used] ||= 0
    cache[:templates_used] += 1

    fast_gen(cache[:templates][cnt][:sql])

    obj = build_class.find cache[:templates][cnt][:id]
  else
    @@sql_inserts = []
    @@record_sql = true
    obj = fg.build name, options
    obj.id = fg.next(:fastory_id_generator) if obj.id.blank? 
    obj.save!
    @@record_sql = false

    @@fastories[cache_key] ||= {}
    @@fastories[cache_key][:templates_available] ||= 0
    @@fastories[cache_key][:templates_available] += 1
    @@fastories[cache_key][:templates_used] = @@fastories[cache_key][:templates_available]

    res = @@fastories[cache_key][:templates] ||= []
    obj_meta = {:sql => @@sql_inserts.dup, :id => obj.id }
    res = @@fastories[cache_key][:templates] << obj_meta 
  end

  obj
end