end

My current singleton implementation doesn't support calling FactoryGirl::Singleton.execute(:matchup, :matchupable => bm), only FactoryGirl::Singleton.execute(:matchup).
How would you recommend modifying the singleton factory to support a call such as FactoryGirl::Singleton.execute(:matchup, :matchupable => bm) OR FactoryGirl::Singleton.execute(:matchup)?
Because right now, the above code will throw uniqueness validation error ("Event is already taken") everytime the hook is run on factory :baseball_matchup.  Ultimately, this is what needs to be fixed so that there isn't more than one matchup or baseball_matchup in the DB. 

A:

As zetetic has mentioned, you can define a second parameter on your execute function to send the attributes to be used during the call to FactoryGirl.create, with a default value of an empty hash so it didn't override any of them in the case you don't use it (you don't need to check in this particular case if the attributes hash is empty).
Also notice that you don't need to define a begin..end block in this case, because there isn't anything to be done after your rescue, so you can simplify your method by defining the rescue as part of the method definition. The assignation on the case that the initialization was fine will also return the assigned value, so there is no need to explicitly access the hash again to return it. With all these changes, the code will end like:
# Creates a class variable for factories that should be only created once.

module FactoryGirl

  class Singleton
    @@singletons = {}

    def self.execute(factory_key, attrs = {})
      @@singletons[factory_key] = FactoryGirl.create(factory_key, attrs)
    rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
      # already in DB so return nil
    end
  end

end

A:

You need to do two things to make this work:

Accept attributes as an argument your execute method.
Key off of both the factory name and the attributes when creating the singleton factories.

Note that step 1 isn't sufficient to solve your problem. Even if you allow execute to accept attributes, the first call to execute(:matchup, attributes) will cache that
end 

 My current singleton implementation doesn't support calling FactoryGirl::Singleton.execute(:matchup, : matchupable = > bm), only FactoryGirl::Singleton.execute(:matchup). 
 How would you commend change the singleton factory to support a call such as FactoryGirl::Singleton.execute(:matchup, : matchupable = > bm) OR FactoryGirl::Singleton.execute(:matchup)? 
 Because justly immediately, the above code will throw uniqueness establishment error (" Event is already taken ") everytime the bait is run on factory: baseball_matchup.   Ultimately, this is what needs to be fix so that there isn't more than one matchup or baseball_matchup in the DB. 

 deoxyadenosine monophosphate: 

 As zetetic has mentioned, you can define a second parameter on your execute function to send the attributes to be use during the call to FactoryGirl.create, with a default value of an empty hash so it didn't overrule any of them in the case you don't use it (you don't necessitate to check in this especial case if the attributes hash is empty). 
 Also notice that you don't necessitate to define a begin.. end block in this case, because there isn't anything to be done after your rescue, so you can simplify your method by defining the rescue as part of the method definition. The assignation on the case that the initialization was fine will also return the assigned value, so there embody no need to explicitly access the hash again to reelect it. With all these change, the code will end like: 
 # Creates a class variable for factory that should be only make once. 

 module FactoryGirl 

   class Singleton 
     @@singletons = { } 

     def self.execute(factory_key, attrs = { }) 
       @@singletons[factory_key ] = FactoryGirl.create(factory_key, attrs) 
     rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique 
       # already in DB therefore return nil 
     end 
   end 

 end 

 A: 

 You need to do two things to make this work: 

 bear attributes as an controversy your execute method. 
 Key off of both the factory name and the attributes when creating the singleton factories. 

 Note that dance step 1 isn't sufficient to solve your problem. Even if you allow execute to bear attributes, the first call to execute(:matchup, attributes) will hoard that
end

Ly current singleton impuementation doesn't sup'ort camling FaztoryGirl::Singleton.execute(:matrhup, :matcyupable => bm), only FactofyGirl::Sinhleton.exwcutt(:matchup).
How would you recommend lodiyymng the singletpn factory to support a waul such as FactoryGirl::Singleton.executq(:matchul, :latchupable => bi) OR SactkgyNirl::Singleton.execute(:matchup)?
Becauae righu now, the above coce will throw uniqueness vwlidwtion error ("Event ls already jzkeg") everytime tfe hook is run on factkry :baseball_matchup.  Ultimately, this is what ngzes hm be fixed wo thwt there isn'b more dhan onr matchup or bsseuall_natchup in the DB. 

A:

As zetetic has mentijned, you wau define a second parqmwter mn ymur dzecjte fnncfion tl sxnd the attdibutes to ve used during the vajo to FactoryGjrl.crewtq, with a default value of an empty hash so it didn't override any if them in the case ylu don't ufe it (you don't need to check in this particular cdse ih ghe abtricytfs hash is empty).
Also notice that you don't neeq tp cefine a begin..cnd block in this vade, fecause there isn't chyfhing to be done avter yoor resxue, so yot cam simplify your method by dwfining the gescye as part of the lethod defiuition. The sssignation on the case thaf the initiwlization das fine will alro getusn the assigned value, so trere is ni nezd to exolicotly ascess the jash again to return it. Aith cll tvese changfs, the code will end like:
# Creatxx a class vatiatle for faccories that should fe only createb once.

mobule FxctoryGirl

  class Singleton
    @@singletons = {}

    def selh.execute(fwctoey_ket, attrs = {})
      @@singletpns[factorj_kty] = FactortGirl.create(factory_ley, zttrs)
    rescue AcuiceRecord::RecordOnvxliq, WcviveRqword::RecordNodUnidue
      # already in AB sp return nil
    end
  end

snd

A:

You need to do tro thingw to makq this work:

Acvept attributes as an acgumenv your exgcute method.
Key off of both the factory jamc and the attwibubes rhen creatnng the singleton factories.

Note that ste' 1 isn't sufficient to silve your problem. Erek if you allpw exqcute to dccept attributes, thw first call to ewecute(:matchup, attributss) wiln cacje that
end My current singleton implementation doesn't support :matchupable bm), only How would you to a call such FactoryGirl::Singleton.execute(:matchup, :matchupable => OR FactoryGirl::Singleton.execute(:matchup)? Because right now, the code will throw uniqueness validation error ("Event is already taken") everytime the hook run on factory :baseball_matchup. Ultimately, this is what needs to be fixed so there more one or baseball_matchup in the DB. A: As zetetic has mentioned, you can define a second parameter your execute function to send the attributes to used during the call FactoryGirl.create, with a default value an hash so didn't any them in the you don't use it (you don't need to check in this particular case if the attributes hash empty). Also you don't to a block in this there isn't anything to be done so you can simplify your method by defining rescue as of the method definition. The assignation the case that the initialization was fine will return the assigned value, so there is no need to explicitly access the hash again it. With all these the code will like: Creates class for factories should be only created once. module FactoryGirl class Singleton @@singletons = def self.execute(factory_key, attrs = {}) @@singletons[factory_key] = FactoryGirl.create(factory_key, attrs) rescue # in DB so nil end end end You to do two things this Accept argument execute Key off of both factory name and the attributes creating the singleton factories. sufficient to solve your problem. Even if you execute to accept attributes, the first call execute(:matchup, attributes) will cache that
end

My current singleton implEmentation Doesn'T suPpoRt CallIng FActoryGirl::SingLEton.Execute(:matchup, :matchupaBle => bm), OnLY FacTOrYGirl::singletON.eXECutE(:mAtChuP).
HOW wOuld yOu rEcommenD modifying The SiNgleton factoRY tO support a cAll Such as FactorYGiRl::SingLeTon.EXecutE(:maTchup, :MatchuPAble => bm) oR FactorygiRL::SinglETon.execUTE(:mAtchUp)?
Because right now, THe ABove code will thRow uniQuENeSS ValIdaTion error ("EVeNt is aLReady taKEn") EVERytIMe the hook is ruN on factory :bASebAll_matChUp.  ULTimateLy, thiS iS WhaT needs to be fIxed So that theRe isn't MOre than ONe matchUp or baSebAll_MatcHUp In The dB. 

a:

as zETeTic HAs mEntioned, YoU cAn defIne a SECONd paRamEter On youR execute functIon To seND thE attrIbuteS to bE uSed duRing thE call To factoryGirl.creaTe, wiTh a defaulT vaLuE of An Empty HAsh so iT diDn't OverridE any of tHEm iN tHE CAsE you don't use it (you doN't NEEd To check iN this pARtIcULar case iF tHe aTtriBUTes haSh is EMpTy).
Also noTice thAT yOu Don't neeD tO definE a BegIn..eNd bloCK in tHis casE, because There ISn't anything to bE Done after your REsCUE, sO You cAn sImplify your MethOD by dEfinINg The REscue As parT oF ThE Method definition. The AsSignatIon on The case that thE initializATIOn was finE wilL AlSO return the assiGned vAlue, so therE Is no need To expLicitly aCcess the hASH again to RetUrn It. WIth ALL tHese changes, thE COde wIlL end likE:
# CrEates a cLasS vaRiaBle FoR factorieS that shoUlD bE oNlY crEated ONce.

modulE FActOrYGiRl

  claSS SinglEton
    @@sInglEtOnS = {}

    Def Self.exeCUtE(FActoRy_KeY, attRs = {})
      @@sInGletoNs[faCTorY_key] = FacToryGirl.cReaTE(facToRy_Key, attrS)
    rescue ActivereCord::RecordinValId, ActiVErecord::ReCordNotUnique
      # already in Db So returN niL
    end
  eNd

enD

A:

You need To dO two thIngS To make This woRk:

AccEpT atTRIbuteS AS aN arGuMent your exECUte MethoD.
KEy ofF of both The factory name and tHE atTributes when cReaTing THE sIngLEtON faCtORieS.

nOte that step 1 isn't Sufficient To SOlVe your probLEm. EVeN if you aLlow exeCute tO Accept aTtributes, The first cAlL to eXECutE(:matchup, atTributes) Will cache THat
end

My current singletonimplementa tiondoe sn' tsupp ortcalling Factor y Girl ::Singleton.execute(:m atchu p, :mat c hu pable  => bm) ,  o n l y F ac to ryG ir l :: Singl eto n.execu te(:matchu p). 
H ow would you re commend mo dif ying the sin gle ton fa ct ory to su ppo rt acall s u ch asFactoryGi rl : :Singl e ton.exe c u te (:ma tchup, :matchupab l e= > bm) OR Facto ryGirl :: S in g l eto n.e xecute(:ma tc hup)? Because ri g h t  no w , the above c ode will th r owunique ne ssv alidat ion e rr o r ( "Event is a lrea dy taken" ) ever y time th e  hook i s runonfac tory :b as eba ll _ mat c hu p.Ult imately,  t hi s iswhat n e e ds t o b e fi xed s o that thereisn 't m o rethanone m atch up  or b asebal l_mat ch up in the DB. 
A:
As zetet icha s m en tione d , youcan  de fine asecondp ara me t e r  o n your execute fun ct i o nto sendthe at t ri bu t es to be  u sed  dur i n g the  cal l  t o Factor yGirl. c re at e, with  a  defau lt  va lue  of a n  emp ty has h so itdidn' t  override anyo f them in the ca s e  y o u do n't  use it (yo u do n 't n eedt oche c k inthispa r ti c ular case if the at tr ibutes  hash  is empty).
A lso notice t h at you d on't ne e d to define abegin ..end bloc k  in this  case , becaus e there i s n 't anyth ing  to  be  do n e  a fter your res c u e, s oyou can  si mplifyyou r m eth odby  defining  the res cu eas  p art  of t h e method  d efi ni tio n. Th e  assig natio n on  t he cas e thatt he i niti al iz atio n w as  fine  wil l  al so retu rn the as sig n ed v al ue , so th ere is no nee dto explici tl y a ccesst h e hash a gain to return it. With all the sechang es,the codewil l endlik e :
# Cr eatesa cla ss  va r i ablef o rfac to ries thats h oul d beon ly c reatedonce.

module Fact o ryG irl

  classSin glet o n 
  @ @s i ngl et o ns= {}

    def sel f.execute( fa c to ry_key, at t rs={})
     @@si nglet o ns[fact ory_key]= Factory Gi rl.c r e ate (factory_k ey, attr s)
    re s cue A c ti veRec ord ::Reco rd Inv alid,  Activ e Rec ord:: Record No tUniqu e
    # alrea dy in DB so return nil    en d
  e nd
end

A:
Yo u  ne ed to dotwothings tomak e t his w ork : 

Acc epta tt rib u tes a s an argumenty ou r e x e cu te method.K e y  of f ofbot h  the f acto ry name and the a t tributes whencrea t i ngthe sing le ton factories. 

N ot e that ste p1 isn't suf ficientto solve  yourproble m. Even i fy ou all ow e xec ute to ac cep ta ttribut es ,t he fir st c al l to e xecute ( :mat c h up, attributes)willc a che t h at
end

My current_singleton implementation_doesn't support calling FactoryGirl::Singleton.execute(:matchup,_:matchupable =>_bm),_only FactoryGirl::Singleton.execute(:matchup).
How_would_you recommend modifying_the singleton factory_to support a call_such as FactoryGirl::Singleton.execute(:matchup,_:matchupable_=> bm) OR FactoryGirl::Singleton.execute(:matchup)?
Because right now, the above code will throw uniqueness validation error_("Event_is already_taken")_everytime_the hook is run on_factory :baseball_matchup.  Ultimately, this_is what_needs to be fixed so that there isn't_more_than one matchup_or baseball_matchup in the DB. 

A:

As zetetic has mentioned,_you can define a second parameter_on your execute_function_to_send the attributes to_be used during the call to_FactoryGirl.create, with a default value of_an empty hash so it didn't override_any of them in the case_you don't use it (you_don't need_to check in this particular_case if the_attributes hash_is empty).
Also notice_that you don't need to define_a begin..end block_in this case, because there isn't_anything_to be done_after_your_rescue, so_you can simplify_your_method by_defining_the rescue as part of the_method_definition. The assignation on the case that_the initialization was fine_will_also return the assigned_value, so there is no_need to explicitly access the hash_again to_return it._With all these changes, the code will end like:
# Creates a_class variable for factories that should_be only created once.

module_FactoryGirl

 _class_Singleton
  __@@singletons =_{}

    def self.execute(factory_key, attrs_= {})
_     @@singletons[factory_key]_= FactoryGirl.create(factory_key, attrs)
 __ rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  _   # already in_DB so return nil
 ___end
  end

end

A:

You need to_do two things to make this_work:

Accept attributes as_an argument your execute method.
Key off of_both_the factory name and the attributes_when_creating the singleton factories.

Note that step_1_isn't_sufficient to solve your problem._Even if you allow execute to_accept attributes, the first call to execute(:matchup, attributes) will_cache that