Skip to content

Instantly share code, notes, and snippets.

@willawill

willawill/gist:4502730

Last active Dec 10, 2015
Embed
What would you like to do?
There are three questions I have.
1. Is it a good practice to massage the data when save it back to the database?
In my case, my params passed back from view looks like {"auto_text"=>"When the wheels come down?", "keyoption"=>{"1"=>{"key"=>"1", "option"=>"2"}}}
and I will break hashes and straighten it to a String which is the :read_text property in Ivr.
I am thinking if I should let Ivr has the key:option pair as it is, and generate the String when needed.
Then I can do presence validation and update later.
2. With Struct way, I don't know how to put it in my model.
3. With long string way, I don't know how to generate and assign back to the property.
Thank you Scott!!!
# class keyOption < Struct.new(:key, :option)
# end
class Ivr
include DataMapper::Resource
property :id, Serial
property :activated, Boolean
property :read_text, Text, lazy: false, required: true
# before :save do |ivr|
# ivr.activated = true
# ivr.read_text = set_read_text
# end
def activate
update(activated: true)
end
def deactivate
update(activated: false)
end
def keyoption=(keyoption)
@keyoption = keyoption.values
end
def auto_text=(auto_text)
@auto_text = auto_text
end
def read_text=
text_to_read = @keyoption.map do |option_hash|
k, v = option_hash.keys.first, option_hash.values.first
elaborate(k) + v.to_s
end.join(' ')
@auto_text + text_to_read
end
# def generate_text(ivr)
# raise "Must be a hash" unless ivr.is_a? Hash
# keyoption = ivr["keyoption"].values
# auto_text = ivr["auto_text"]
# text_to_read = keyoption.map do |option_hash|
# k, v = option_hash.keys.first, option_hash.values.first
# elaborate(k) + v.to_s
# end.join(' ')
# auto_text + text_to_read
# end
private
def elaborate(key)
case key
when 'key'
'Press '
when 'option'
'for '
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment