Begin Rescue Refactor - ruby-on-rails

I've had to use begin rescue way to many times, and I want to refactor but I can't create a method where I pass a parameter that uses the .parent method.
This is because .parent is the method that raises the error, but that is the only thing that is changing and I don't know how to write the code without calling the method
I do want to get the parent name, but if there is no parent that is why I get the error. So i essentially need it to return a empty string when there is no parent, but I don't know how to recursively add a .parent.:
def check_parent_name(object)
if ['body','html', 'head', 'document'].include?(object.parent.name)
''
else
object.parent.content
end
end
begin
parent_content = check_parent_name(img.parent)
rescue => e
parent_content = ''
end
begin
parent_parent_content = check_parent_name(img.parent.parent)
rescue => e
parent_parent_content = ''
end
begin
parent_parent_parent_content = check_parent_name(img.parent.parent.parent)
rescue => e
parent_parent_parent_content = ''
end
begin
parent_parent_parent_parent_content = check_parent_name(img.parent.parent.parent.parent)
rescue => e
parent_parent_parent_parent_content = ''
end
begin
parent_parent_parent_parent_parent_content = check_parent_name(img.parent.parent.parent.parent.parent)
rescue => e
parent_parent_parent_parent_parent_content = ''
end

A very simple way is to use directly rescue inside your statement:
parent_content = check_parent_name(img.parent) rescue ""
But this is not the way you should code.
Like DaveNewton said, we need to know what use of this code you want, and we could help you to find a better, cleaner and more flexible way to implement the feature!
It seems that you have a structure as Tree (parent-children) between your records, am I wrong?
A recursive method (= calling itself) is propably what you want here, something like:
def your_method(img)
return "" unless img.present?
if img.try(:parent).present?
your_method(img.parent)
else
return check_parent_name(img)
end
end
But I don't know for sure it is what you want...

Related

Parsing the text file in ruby

My text file looks like this
VOTE 1168041805 Campaign:ssss_uk_01B Validity:during Choice:Antony CONN:MIG01TU MSISDN:00777778359999 GUID:E6109CA1-7756-45DC-8EE7-677CA7C3D7F3 Shortcode:63334
VOTE 1168041837 Campaign:ssss_uk_01B Validity:during Choice:Leon CONN:MIG00VU MSISDN:00777770939999 GUID:88B52A7B-A182-405C-9AE6-36FCF2E47294 Shortcode:63334
I want to get value of vote campaign validity choice for which I am doing this:
File.foreach('lib/data/file.txt') do |line|
line = line.tidy_bytes
begin
aline = line.match(/^VOTE\s(\d+)\sCampaign:([^ ]+)\sValidity:([^ ]+)\sChoice:([^ ]+)/)
unless aline.nil?
## do something
end
rescue Exception => e
raise " error: " + e.inspect
p line.inspect
next
end
end
Is there any better way for doing this for
aline = line.match(/^VOTE\s(\d+)\sCampaign:([^ ]+)\sValidity:([^ ]+)\sChoice:([^ ]+)/)
and getting aline[1] aline[2] aline[3] and aline[4]
You can use named captures to get a hash of results instead:
# use a freezed contant instead of making a new Regexp object for each line
REGEXP = /^VOTE\s(?<id>\d+)\sCampaign:(?<campaign>[^ ]+)\sValidity:(?<validity>[^ ]+)\sChoice:(?<choice>[^ ]+)/.freeze
File.foreach('lib/data/file.txt') do |line|
begin
matches = line.tidy_bytes.match(REGEXP)
hash = matches.names.zip(matches.captures).to_h
end
rescue Exception => e
raise " error: " + e.inspect
p line.inspect
next
end
end
If the desired result is an array you might want to use .map:
# use a freezed contant instead of making a new Regexp object for each line
REGEXP = /^VOTE\s(?<id>\d+)\sCampaign:(?<campaign>[^ ]+)\sValidity:(?<validity>[^ ]+)\sChoice:(?<choice>[^ ]+)/.freeze
results = File.foreach('lib/data/file.txt').map do |line|
matches = line.tidy_bytes.match(REGEXP)
matches.names.zip(matches.captures).to_h
end

Is there a method to set a value in rails to nil and save?

What I'm thinking of is something where I can say:
e = Foo.new
e.bar = "hello"
e.save
e.reload
e.bar.nil!
e.reload
e.bar.nil? => true
Kind of #touch but sets nil and saves.
EDIT
Super sorry guys. I mean this:
e = Foo.new
e.bar = "hello"
e.save
e.reload
e.bar.nil!
e.reload
e.bar.nil? => true
Maybe something like:
module ActiveRecord
class Base
def nil!(*names)
unless persisted?
raise ActiveRecordError, <<-MSG.squish
cannot nil on a new or destroyed record object. Consider using
persisted?, new_record?, or destroyed? before nilling
MSG
end
unless names.empty?
changes = {}
names.each do |column|
column = column.to_s
changes[column] = write_attribute(column, nil)
end
primary_key = self.class.primary_key
scope = self.class.unscoped.where(primary_key => _read_attribute(primary_key))
if locking_enabled?
locking_column = self.class.locking_column
scope = scope.where(locking_column => _read_attribute(locking_column))
changes[locking_column] = increment_lock
end
clear_attribute_changes(changes.keys)
result = scope.update_all(changes) == 1
if !result && locking_enabled?
raise ActiveRecord::StaleObjectError.new(self, "nil")
end
#_trigger_update_callback = result
result
else
true
end
end
end
end
Put that in an initializer and it'll let you null out the title of a comment with Comment.last.nil!(:title).
You can't save a nil to the database, and furthermore, once an object has been created as a particular class you can never change that. It can only be converted by creating a new object, something an in-place modifier like this hypothetical nil! does.
The closest thing you can get is:
e = Foo.new
e.bar = "hello"
e.save
e.reload
e.delete!
e.reload
e.destroyed? # => true
f = Foo.find_by(id: e.id)
f.nil? # => true

Return save outcome inside begin/ensure block

I've a setter method for an attr_accessor in rails
# setter method of the shopify_p accessor
def shopify_v=(s)
begin
self.product.shop.connect_to_store
#shopify_v = s if s.save
ensure
ShopifyAPI::Base.site = nil
end
end
I'd like it to return true if save is successful or false if the save action doesn't work.
Instead it always outputs the s object (or #shopify_v, I don't know).
How can I make it return true or false depending on the save action?
Thanks,
Augusto
UPDATE #1
Here is the getter method of the same attr_accessor.
Basically it downloads the object from the server only in case it has never done it before.
# getter method of the shopify_v accessor
def shopify_v
if #shopify_v.nil?
begin
self.product.shop.connect_to_store
#shopify_v = ShopifyAPI::Variant.find(self.shopify_id)
ensure
ShopifyAPI::Base.site = nil
end
puts "remote"
return #shopify_v
else
puts "local"
return #shopify_v
end
end
def shopify_v=(s)
begin
self.product.shop.connect_to_store
#shopify_v = s if s.save
ensure
ShopifyAPI::Base.site = nil
end
#shopify_v.present? # will return true or false
end
I would use exceptions, since being unable to save is an exceptional condition. Also, whenever practical, a method should either have a side-effect, or return a value.
For example:
class ShopifyError < StandardError ; end
def shopify_v=(s)
begin
self.product.shop.connect_to_store
raise ShopifyError unless s.save
#shopify_v = s
ensure
ShopifyAPI::Base.site = nil
end
end
and in the caller:
begin
...
model.v = s
...
rescue ShopifyError
# set flash[:notify], or whatever error handling is appropriate
end
Also, there are cleaner ways to structure the getter. Consider doing something like this:
# getter method of the shopify_v accessor
def shopify_v
#shopify_v ||= fetch_v
end
private
def fetch_v
begin
self.product.shop.connect_to_store
ShopifyAPI::Variant.find(self.shopify_id)
ensure
ShopifyAPI::Base.site = nil
end
end
A setter always returns the value being set, no matter what you try to return. So you should use another method name, for example:
def set_shopify_v(s)
self.product.shop.connect_to_store
status = s.save
#shopify_v = s if status
status
rescue => exc
# Rails.logger.error(...)
false
ensure
ShopifyAPI::Base.site = nil
end

Rails - Triggering Flash Warning with method returning true

I'm trying to trigger a warning when a price is entered too low. But for some reason, it always returns true and I see the warning regardless. I'm sure there something wrong in the way I'm doing this as I'm really new to RoR.
In model:
def self.too_low(value)
res = Class.find_by_sql("SELECT price ……. WHERE value = '#{value}'")
res.each do |v|
if #{value} < v.price.round(2)
return true
else
return false
end
end
end
In controller:
#too_low = Class.too_low(params[:amount])
if #too_low == true
flash[:warning] = 'Price is too low.'
end
I would write it somewhat different. You iterate over all items, but you are only interested in the first element. You return from inside the iteration block, but for each element the block will be executed. In ruby 1.9.2 this gives an error.
Also i would propose using a different class-name (Class is used to define a class)
So my suggestion:
Class YourGoodClassName
def self.too_low(amount)
res = YourGoodClassName.find_by_sql(...)
if res.size > 0
res[0].price.round(2) < 1.00
else
true
end
end
end
You can see i test if any result is found, and if it is i just return the value of the test (which is true or false); and return true if no price was found.
In the controller you write something like
flash[:warning] = 'Price is too low' if YourGoodClassName.too_low(params[:amount])

RubyAmf and Rails 3

I have recently been trying to upgrade my app form Rails 2.3.8 to newly-releases Rails 3.
After going through fixing some Rails 3 RubyAMF doesn't seem to work:
>>>>>>>> RubyAMF >>>>>>>>> #<RubyAMF::Actions::PrepareAction:0x1649924> took: 0.00017 secs
The action '#<ActionDispatch::Request:0x15c0cf0>' could not be found for DaysController
/Users/tammam56/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/base.rb:114:in `process'
/Users/tammam56/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:40:in `process'
It doesn't seem to be able to find the proper controller. Might have to do with new changes in Rails 3 Router. Do you know how to go about finding the root cause of the problem and/or trying to fix it?
I'm pasting code from RubyAMF where this is happening (Exception happens at the line: #service.process(req, res)):
#invoke the service call
def invoke
begin
# RequestStore.available_services[#amfbody.service_class_name] ||=
#service = #amfbody.service_class_name.constantize.new #handle on service
rescue Exception => e
puts e.message
puts e.backtrace
raise RUBYAMFException.new(RUBYAMFException.UNDEFINED_OBJECT_REFERENCE_ERROR, "There was an error loading the service class #{#amfbody.service_class_name}")
end
if #service.private_methods.include?(#amfbody.service_method_name.to_sym)
raise RUBYAMFExc
eption.new(RUBYAMFException.METHOD_ACCESS_ERROR, "The method {#{#amfbody.service_method_name}} in class {#{#amfbody.service_class_file_path}} is declared as private, it must be defined as public to access it.")
elsif !#service.public_methods.include?(#amfbody.service_method_name.to_sym)
raise RUBYAMFException.new(RUBYAMFException.METHOD_UNDEFINED_METHOD_ERROR, "The method {#{#amfbody.service_method_name}} in class {#{#amfbody.service_class_file_path}} is not declared.")
end
#clone the request and response and alter it for the target controller/method
req = RequestStore.rails_request.clone
res = RequestStore.rails_response.clone
#change the request controller/action targets and tell the service to process. THIS IS THE VOODOO. SWEET!
controller = #amfbody.service_class_name.gsub("Controller","").underscore
action = #amfbody.service_method_name
req.parameters['controller'] = req.request_parameters['controller'] = req.path_parameters['controller'] = controller
req.parameters['action'] = req.request_parameters['action'] = req.path_parameters['action'] = action
req.env['PATH_INFO'] = req.env['REQUEST_PATH'] = req.env['REQUEST_URI'] = "#{controller}/#{action}"
req.env['HTTP_ACCEPT'] = 'application/x-amf,' + req.env['HTTP_ACCEPT'].to_s
#set conditional helper
#service.is_amf = true
#service.is_rubyamf = true
#process the request
rubyamf_params = #service.rubyamf_params = {}
if #amfbody.value && !#amfbody.value.empty?
#amfbody.value.each_with_index do |item,i|
rubyamf_params[i] = item
end
end
# put them by default into the parameter hash if they opt for it
rubyamf_params.each{|k,v| req.parameters[k] = v} if ParameterMappings.always_add_to_params
begin
#One last update of the parameters hash, this will map custom mappings to the hash, and will override any conflicting from above
ParameterMappings.update_request_parameters(#amfbody.service_class_name, #amfbody.service_method_name, req.parameters, rubyamf_params, #amfbody.value)
rescue Exception => e
raise RUBYAMFException.new(RUBYAMFException.PARAMETER_MAPPING_ERROR, "There was an error with your parameter mappings: {#{e.message}}")
end
#service.process(req, res)
#unset conditional helper
#service.is_amf = false
#service.is_rubyamf = false
#service.rubyamf_params = rubyamf_params # add the rubyamf_args into the controller to be accessed
result = RequestStore.render_amf_results
#handle FaultObjects
if result.class.to_s == 'FaultObject' #catch returned FaultObjects - use this check so we don't have to include the fault object module
e = RUBYAMFException.new(result['code'], result['message'])
e.payload = result['payload']
raise e
end
#amf3
#amfbody.results = result
if #amfbody.special_handling == 'RemotingMessage'
#wrapper = generate_acknowledge_object(#amfbody.get_meta('messageId'), #amfbody.get_meta('clientId'))
#wrapper["body"] = result
#amfbody.results = #wrapper
end
#amfbody.success! #set the success response uri flag (/onResult)
end
The best suggestion is to try rails3-amf. It currently is severely lacking in features in comparison to RubyAMF, but it does work and I'm adding new features as soon as they are requested or I have time.

Resources