How can I build this recursive method in rails/ruby? -


i'm using form object pattern in rails, , generating data map consists of nested form objects want commit db.

for example, have this:

  • productformobject
    • featureformobject1
      • keyformobject (ex: color)
      • valueformobject (ex: red)
    • featureformobject2
      • keyformobject (ex: size)
      • valueformobject (ex: large)

so recursively validate data, need start bottom up.

in other words, product without features invalid, before can validate product need validate features. feature without key/value pair (ex: color = red) invalid. need first validated key , value form objects.

i figure can set state of each form object having been validated or not. i'm not sure how work bottom-up.

i don't have code worth sharing, i'm in process of building out now. pseudo-code too, , i'll post final results once working.

i think you're overcomplicating things - doesn't need recursive or bottom up.

on productformobject have like

def valid?    feature_form_objects.any? && feature_form_objects.all?(&:valid?) end 

to there must @ least 1 child object , must valid.

and valid? method on featureformobject needs verify both key , value valid? (possibly first checking not nil - depend on how construct things may not possible). might need check combination of key , value valid.

def valid?   key_form_object.valid? && value_form_object.valid? end 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -