Skip to content

Instantly share code, notes, and snippets.

@piotrze
Created September 19, 2015 09:37
Show Gist options
  • Save piotrze/6206f4d4be736bdcecd6 to your computer and use it in GitHub Desktop.
Save piotrze/6206f4d4be736bdcecd6 to your computer and use it in GitHub Desktop.
Little refactor
class CustomFields
def initialize
@hash = {}
end
def []=(key, value)
@hash[key] = value
end
def to_hash
change_booleans
end
private
def change_booleans
new_hash = {}
@hash.each do |k, v|
if v.is_a?(TrueClass) || v.is_a?(FalseClass)
new_hash[k] = v ? 'Yes' : 'No'
else
new_hash[k] = v
end
end
return new_hash
end
end
# Before
custom_fields = {}
custom_fields['Postcode'] = s.postcode
custom_fields['TestAlreadyBooked'] = s.test_booked ? 'Yes' : 'No'
custom_fields['PaidSubscriber'] = s.latest_subscription ? 'Yes' : 'No'
api(custom_fields)
# After
custom_fields = CustomFields.new
custom_fields['Postcode'] = s.postcode
custom_fields['TestAlreadyBooked'] = s.test_booked?
custom_fields['PaidSubscriber'] = s.latest_subscription?
api(custom_fields.to_h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment