Use of inheritance using two Ruby libraries
up vote
1
down vote
favorite
Currently I'm working on parsing data from a form service into a pdf form. I created 2 classes one inheriting from the other one. However, I see that the classes I created are growing in lines of code and I'm concern that I am concern that these classes become hard to maintain.
I would like to request feedback from the community regarding OOP principles, style violations and best practices. If there are also security concerns points those out. Here are the classes using inheritance.
require 'open-uri'
class FillablePdfForm
attr_writer :template_path
attr_reader :attributes
def initialize
fill_out
end
def export(file_name, output_file_path: nil)
output_file_path ||= "#{Rails.root}/tmp/pdfs/#{application_date}_#{which_application}_#{what_status}_#{file_name}.pdf"
pdftk.fill_form template_path, output_file_path, attributes
output_file_path
end
def form_fields
pdftk.get_field_names template_path
end
def template_path
pdf_form_application
@template_path ||=
"#{Rails.root}/public/pdfs/#{which_application.downcase}_#{what_status.downcase}.pdf"
end
protected
def application_date
Time.now.strftime('%Y-%m-%d')
end
def pdf_form_application
File.open("#{Rails.root}/public/pdfs/#{which_application.downcase}_#{what_status.downcase}.pdf", "wb") do |file|
file << URI.parse(url_of_pdf_form).read
end
end
def url_of_pdf_form
@form_fields.find do |field|
field['label'] == "#{which_application}_#{what_status}_URL"
end['default']
end
def attributes
@attributes ||= {}
end
def fill(key, value)
attributes[key.to_s] = value
end
def pdftk
@pdftk ||= PdfForms.new
end
def fill_out
raise 'Must be overridden by child class'
end
end
Also, I'm passing in the constructor FormStack::Form.new
but I was wondering if I should pass it as an argument.
class PdfScrie < FillablePdfForm
def initialize(user_submission_data, form_fields)
@user_submission_data = user_submission_data
@form_fields = form_fields
@formstack = FormStack::Form.new
super()
end
private
# PDF Constants
VALUE_CHECKBOX_ON = 'On'.freeze
OPTION_SEP = ' | '.freeze
LABEL_APPLICATION = 'APPLICATION'.freeze
LABEL_STATUS = 'STATUS'.freeze
def fill_out
form_fields.each do |field| # PDF form fields
unless dictionary[field]
Rails.logger.warn "#{self.class.name}: Missing "#{field}" mapping."
next
end
id = dictionary[field].split(OPTION_SEP)[0]
@user_submission_data
.select { |field_data| field_data[FormStack::Form::ATTR_FIELD_ID] == id }
.each { |field_data| fill_form_with_data(field, field_data) }
end
end
def fill_form_with_data(field, field_data)
field_number = field_data[FormStack::Form::ATTR_FIELD_ID]
value = field_data[FormStack::Form::ATTR_FIELD_VALUE]
field_type = FormStack::Form::field_type(@form_fields, field_number)
self_method = "fill_#{field_type}".to_sym
if self.respond_to?(self_method, :include_private)
send(self_method, field_number, field, value)
else
fill(field, value)
end
end
# Field Type Methods
def fill_address(field_number, field, value)
address_by_section = FormStack::Form.parse_formstack_nested_attrs(value)
address_by_section.each do |section, value|
fill(field, value) if form_field_has_section?(field, section) ||
FormStack::Form::address_section_aparment?(field, section)
end
end
def fill_phone(field_number, field, value)
parse_phone_number(value)
fill(field, @phone_number_sections.shift)
end
def fill_name(field_number, field, value)
full_name = FormStack::Form::parse_name(value)
fill(field, full_name)
end
def fill_checkbox(field_number, field, value)
if FormStack::Form::field_is_grouped_checkbox(@form_fields, field_number)
FormStack::Form::parse_checked_options(value).each do |option|
fill(field, VALUE_CHECKBOX_ON) if checked_option_matches_value(field, option)
end
else
fill(field, value)
end
end
# END Field Type Methods
# Helpers
def checked_option_matches_value(field, option)
dictionary[field].split(OPTION_SEP)[1].include?(option)
end
def parse_phone_number(phone_number)
if phone_number_sections_empty?
@phone_number_sections = FormStack::Form::parse_phone(phone_number)
end
end
def phone_number_sections_empty?
@phone_number_sections.nil? || @phone_number_sections.empty?
end
def form_field_has_section?(form_field_name, address_section)
form_field_name.include? address_section.upcase
end
def dictionary
@dictionary ||= JSON.parse(find_dictionary['section_text'])
end
def find_dictionary
@formstack.find_field_by_label("#{which_application}_#{what_status}_DICTIONARY",
@form_fields)
end
def which_application
@formstack.find_value_by_label(LABEL_APPLICATION,
@form_fields,
@user_submission_data)
end
def what_status
@formstack
.find_value_by_label(LABEL_STATUS, @form_fields, @user_submission_data)
end
end
Feel free to point out areas of improvement, feedback, best practices and resources.
object-oriented ruby design-patterns
New contributor
add a comment |
up vote
1
down vote
favorite
Currently I'm working on parsing data from a form service into a pdf form. I created 2 classes one inheriting from the other one. However, I see that the classes I created are growing in lines of code and I'm concern that I am concern that these classes become hard to maintain.
I would like to request feedback from the community regarding OOP principles, style violations and best practices. If there are also security concerns points those out. Here are the classes using inheritance.
require 'open-uri'
class FillablePdfForm
attr_writer :template_path
attr_reader :attributes
def initialize
fill_out
end
def export(file_name, output_file_path: nil)
output_file_path ||= "#{Rails.root}/tmp/pdfs/#{application_date}_#{which_application}_#{what_status}_#{file_name}.pdf"
pdftk.fill_form template_path, output_file_path, attributes
output_file_path
end
def form_fields
pdftk.get_field_names template_path
end
def template_path
pdf_form_application
@template_path ||=
"#{Rails.root}/public/pdfs/#{which_application.downcase}_#{what_status.downcase}.pdf"
end
protected
def application_date
Time.now.strftime('%Y-%m-%d')
end
def pdf_form_application
File.open("#{Rails.root}/public/pdfs/#{which_application.downcase}_#{what_status.downcase}.pdf", "wb") do |file|
file << URI.parse(url_of_pdf_form).read
end
end
def url_of_pdf_form
@form_fields.find do |field|
field['label'] == "#{which_application}_#{what_status}_URL"
end['default']
end
def attributes
@attributes ||= {}
end
def fill(key, value)
attributes[key.to_s] = value
end
def pdftk
@pdftk ||= PdfForms.new
end
def fill_out
raise 'Must be overridden by child class'
end
end
Also, I'm passing in the constructor FormStack::Form.new
but I was wondering if I should pass it as an argument.
class PdfScrie < FillablePdfForm
def initialize(user_submission_data, form_fields)
@user_submission_data = user_submission_data
@form_fields = form_fields
@formstack = FormStack::Form.new
super()
end
private
# PDF Constants
VALUE_CHECKBOX_ON = 'On'.freeze
OPTION_SEP = ' | '.freeze
LABEL_APPLICATION = 'APPLICATION'.freeze
LABEL_STATUS = 'STATUS'.freeze
def fill_out
form_fields.each do |field| # PDF form fields
unless dictionary[field]
Rails.logger.warn "#{self.class.name}: Missing "#{field}" mapping."
next
end
id = dictionary[field].split(OPTION_SEP)[0]
@user_submission_data
.select { |field_data| field_data[FormStack::Form::ATTR_FIELD_ID] == id }
.each { |field_data| fill_form_with_data(field, field_data) }
end
end
def fill_form_with_data(field, field_data)
field_number = field_data[FormStack::Form::ATTR_FIELD_ID]
value = field_data[FormStack::Form::ATTR_FIELD_VALUE]
field_type = FormStack::Form::field_type(@form_fields, field_number)
self_method = "fill_#{field_type}".to_sym
if self.respond_to?(self_method, :include_private)
send(self_method, field_number, field, value)
else
fill(field, value)
end
end
# Field Type Methods
def fill_address(field_number, field, value)
address_by_section = FormStack::Form.parse_formstack_nested_attrs(value)
address_by_section.each do |section, value|
fill(field, value) if form_field_has_section?(field, section) ||
FormStack::Form::address_section_aparment?(field, section)
end
end
def fill_phone(field_number, field, value)
parse_phone_number(value)
fill(field, @phone_number_sections.shift)
end
def fill_name(field_number, field, value)
full_name = FormStack::Form::parse_name(value)
fill(field, full_name)
end
def fill_checkbox(field_number, field, value)
if FormStack::Form::field_is_grouped_checkbox(@form_fields, field_number)
FormStack::Form::parse_checked_options(value).each do |option|
fill(field, VALUE_CHECKBOX_ON) if checked_option_matches_value(field, option)
end
else
fill(field, value)
end
end
# END Field Type Methods
# Helpers
def checked_option_matches_value(field, option)
dictionary[field].split(OPTION_SEP)[1].include?(option)
end
def parse_phone_number(phone_number)
if phone_number_sections_empty?
@phone_number_sections = FormStack::Form::parse_phone(phone_number)
end
end
def phone_number_sections_empty?
@phone_number_sections.nil? || @phone_number_sections.empty?
end
def form_field_has_section?(form_field_name, address_section)
form_field_name.include? address_section.upcase
end
def dictionary
@dictionary ||= JSON.parse(find_dictionary['section_text'])
end
def find_dictionary
@formstack.find_field_by_label("#{which_application}_#{what_status}_DICTIONARY",
@form_fields)
end
def which_application
@formstack.find_value_by_label(LABEL_APPLICATION,
@form_fields,
@user_submission_data)
end
def what_status
@formstack
.find_value_by_label(LABEL_STATUS, @form_fields, @user_submission_data)
end
end
Feel free to point out areas of improvement, feedback, best practices and resources.
object-oriented ruby design-patterns
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Currently I'm working on parsing data from a form service into a pdf form. I created 2 classes one inheriting from the other one. However, I see that the classes I created are growing in lines of code and I'm concern that I am concern that these classes become hard to maintain.
I would like to request feedback from the community regarding OOP principles, style violations and best practices. If there are also security concerns points those out. Here are the classes using inheritance.
require 'open-uri'
class FillablePdfForm
attr_writer :template_path
attr_reader :attributes
def initialize
fill_out
end
def export(file_name, output_file_path: nil)
output_file_path ||= "#{Rails.root}/tmp/pdfs/#{application_date}_#{which_application}_#{what_status}_#{file_name}.pdf"
pdftk.fill_form template_path, output_file_path, attributes
output_file_path
end
def form_fields
pdftk.get_field_names template_path
end
def template_path
pdf_form_application
@template_path ||=
"#{Rails.root}/public/pdfs/#{which_application.downcase}_#{what_status.downcase}.pdf"
end
protected
def application_date
Time.now.strftime('%Y-%m-%d')
end
def pdf_form_application
File.open("#{Rails.root}/public/pdfs/#{which_application.downcase}_#{what_status.downcase}.pdf", "wb") do |file|
file << URI.parse(url_of_pdf_form).read
end
end
def url_of_pdf_form
@form_fields.find do |field|
field['label'] == "#{which_application}_#{what_status}_URL"
end['default']
end
def attributes
@attributes ||= {}
end
def fill(key, value)
attributes[key.to_s] = value
end
def pdftk
@pdftk ||= PdfForms.new
end
def fill_out
raise 'Must be overridden by child class'
end
end
Also, I'm passing in the constructor FormStack::Form.new
but I was wondering if I should pass it as an argument.
class PdfScrie < FillablePdfForm
def initialize(user_submission_data, form_fields)
@user_submission_data = user_submission_data
@form_fields = form_fields
@formstack = FormStack::Form.new
super()
end
private
# PDF Constants
VALUE_CHECKBOX_ON = 'On'.freeze
OPTION_SEP = ' | '.freeze
LABEL_APPLICATION = 'APPLICATION'.freeze
LABEL_STATUS = 'STATUS'.freeze
def fill_out
form_fields.each do |field| # PDF form fields
unless dictionary[field]
Rails.logger.warn "#{self.class.name}: Missing "#{field}" mapping."
next
end
id = dictionary[field].split(OPTION_SEP)[0]
@user_submission_data
.select { |field_data| field_data[FormStack::Form::ATTR_FIELD_ID] == id }
.each { |field_data| fill_form_with_data(field, field_data) }
end
end
def fill_form_with_data(field, field_data)
field_number = field_data[FormStack::Form::ATTR_FIELD_ID]
value = field_data[FormStack::Form::ATTR_FIELD_VALUE]
field_type = FormStack::Form::field_type(@form_fields, field_number)
self_method = "fill_#{field_type}".to_sym
if self.respond_to?(self_method, :include_private)
send(self_method, field_number, field, value)
else
fill(field, value)
end
end
# Field Type Methods
def fill_address(field_number, field, value)
address_by_section = FormStack::Form.parse_formstack_nested_attrs(value)
address_by_section.each do |section, value|
fill(field, value) if form_field_has_section?(field, section) ||
FormStack::Form::address_section_aparment?(field, section)
end
end
def fill_phone(field_number, field, value)
parse_phone_number(value)
fill(field, @phone_number_sections.shift)
end
def fill_name(field_number, field, value)
full_name = FormStack::Form::parse_name(value)
fill(field, full_name)
end
def fill_checkbox(field_number, field, value)
if FormStack::Form::field_is_grouped_checkbox(@form_fields, field_number)
FormStack::Form::parse_checked_options(value).each do |option|
fill(field, VALUE_CHECKBOX_ON) if checked_option_matches_value(field, option)
end
else
fill(field, value)
end
end
# END Field Type Methods
# Helpers
def checked_option_matches_value(field, option)
dictionary[field].split(OPTION_SEP)[1].include?(option)
end
def parse_phone_number(phone_number)
if phone_number_sections_empty?
@phone_number_sections = FormStack::Form::parse_phone(phone_number)
end
end
def phone_number_sections_empty?
@phone_number_sections.nil? || @phone_number_sections.empty?
end
def form_field_has_section?(form_field_name, address_section)
form_field_name.include? address_section.upcase
end
def dictionary
@dictionary ||= JSON.parse(find_dictionary['section_text'])
end
def find_dictionary
@formstack.find_field_by_label("#{which_application}_#{what_status}_DICTIONARY",
@form_fields)
end
def which_application
@formstack.find_value_by_label(LABEL_APPLICATION,
@form_fields,
@user_submission_data)
end
def what_status
@formstack
.find_value_by_label(LABEL_STATUS, @form_fields, @user_submission_data)
end
end
Feel free to point out areas of improvement, feedback, best practices and resources.
object-oriented ruby design-patterns
New contributor
Currently I'm working on parsing data from a form service into a pdf form. I created 2 classes one inheriting from the other one. However, I see that the classes I created are growing in lines of code and I'm concern that I am concern that these classes become hard to maintain.
I would like to request feedback from the community regarding OOP principles, style violations and best practices. If there are also security concerns points those out. Here are the classes using inheritance.
require 'open-uri'
class FillablePdfForm
attr_writer :template_path
attr_reader :attributes
def initialize
fill_out
end
def export(file_name, output_file_path: nil)
output_file_path ||= "#{Rails.root}/tmp/pdfs/#{application_date}_#{which_application}_#{what_status}_#{file_name}.pdf"
pdftk.fill_form template_path, output_file_path, attributes
output_file_path
end
def form_fields
pdftk.get_field_names template_path
end
def template_path
pdf_form_application
@template_path ||=
"#{Rails.root}/public/pdfs/#{which_application.downcase}_#{what_status.downcase}.pdf"
end
protected
def application_date
Time.now.strftime('%Y-%m-%d')
end
def pdf_form_application
File.open("#{Rails.root}/public/pdfs/#{which_application.downcase}_#{what_status.downcase}.pdf", "wb") do |file|
file << URI.parse(url_of_pdf_form).read
end
end
def url_of_pdf_form
@form_fields.find do |field|
field['label'] == "#{which_application}_#{what_status}_URL"
end['default']
end
def attributes
@attributes ||= {}
end
def fill(key, value)
attributes[key.to_s] = value
end
def pdftk
@pdftk ||= PdfForms.new
end
def fill_out
raise 'Must be overridden by child class'
end
end
Also, I'm passing in the constructor FormStack::Form.new
but I was wondering if I should pass it as an argument.
class PdfScrie < FillablePdfForm
def initialize(user_submission_data, form_fields)
@user_submission_data = user_submission_data
@form_fields = form_fields
@formstack = FormStack::Form.new
super()
end
private
# PDF Constants
VALUE_CHECKBOX_ON = 'On'.freeze
OPTION_SEP = ' | '.freeze
LABEL_APPLICATION = 'APPLICATION'.freeze
LABEL_STATUS = 'STATUS'.freeze
def fill_out
form_fields.each do |field| # PDF form fields
unless dictionary[field]
Rails.logger.warn "#{self.class.name}: Missing "#{field}" mapping."
next
end
id = dictionary[field].split(OPTION_SEP)[0]
@user_submission_data
.select { |field_data| field_data[FormStack::Form::ATTR_FIELD_ID] == id }
.each { |field_data| fill_form_with_data(field, field_data) }
end
end
def fill_form_with_data(field, field_data)
field_number = field_data[FormStack::Form::ATTR_FIELD_ID]
value = field_data[FormStack::Form::ATTR_FIELD_VALUE]
field_type = FormStack::Form::field_type(@form_fields, field_number)
self_method = "fill_#{field_type}".to_sym
if self.respond_to?(self_method, :include_private)
send(self_method, field_number, field, value)
else
fill(field, value)
end
end
# Field Type Methods
def fill_address(field_number, field, value)
address_by_section = FormStack::Form.parse_formstack_nested_attrs(value)
address_by_section.each do |section, value|
fill(field, value) if form_field_has_section?(field, section) ||
FormStack::Form::address_section_aparment?(field, section)
end
end
def fill_phone(field_number, field, value)
parse_phone_number(value)
fill(field, @phone_number_sections.shift)
end
def fill_name(field_number, field, value)
full_name = FormStack::Form::parse_name(value)
fill(field, full_name)
end
def fill_checkbox(field_number, field, value)
if FormStack::Form::field_is_grouped_checkbox(@form_fields, field_number)
FormStack::Form::parse_checked_options(value).each do |option|
fill(field, VALUE_CHECKBOX_ON) if checked_option_matches_value(field, option)
end
else
fill(field, value)
end
end
# END Field Type Methods
# Helpers
def checked_option_matches_value(field, option)
dictionary[field].split(OPTION_SEP)[1].include?(option)
end
def parse_phone_number(phone_number)
if phone_number_sections_empty?
@phone_number_sections = FormStack::Form::parse_phone(phone_number)
end
end
def phone_number_sections_empty?
@phone_number_sections.nil? || @phone_number_sections.empty?
end
def form_field_has_section?(form_field_name, address_section)
form_field_name.include? address_section.upcase
end
def dictionary
@dictionary ||= JSON.parse(find_dictionary['section_text'])
end
def find_dictionary
@formstack.find_field_by_label("#{which_application}_#{what_status}_DICTIONARY",
@form_fields)
end
def which_application
@formstack.find_value_by_label(LABEL_APPLICATION,
@form_fields,
@user_submission_data)
end
def what_status
@formstack
.find_value_by_label(LABEL_STATUS, @form_fields, @user_submission_data)
end
end
Feel free to point out areas of improvement, feedback, best practices and resources.
object-oriented ruby design-patterns
object-oriented ruby design-patterns
New contributor
New contributor
New contributor
asked 14 hours ago
Steven Aguilar
1062
1062
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Steven Aguilar is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209536%2fuse-of-inheritance-using-two-ruby-libraries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Steven Aguilar is a new contributor. Be nice, and check out our Code of Conduct.
Steven Aguilar is a new contributor. Be nice, and check out our Code of Conduct.
Steven Aguilar is a new contributor. Be nice, and check out our Code of Conduct.
Steven Aguilar is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209536%2fuse-of-inheritance-using-two-ruby-libraries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown