Share
Embed
Sprinkles of Functional Programming
Often in Rails apps there is a need to accomplish tasks where we are less interested in modeling domain concepts as collections of related objects and more interested in transforming or aggregating data. Instead of forcing object oriented design into a role that it wasn’t intended for, we can lean into ruby’s functional capabilities to create Rails apps with sprinkles of functional code to accomplish these tasks.
johnschoeman
April 30, 2019
Transcript
109 slides
-
Roadmap Thesis FP and OO A Recommendation A Story with
Some Code Recap / Action Item Questions johnschoeman -
class A < B Object.new define_method(:foo) -> (x) { |x|
x + 1 } data.map yield_self / then johnschoeman -
We should choose our programming style based off the task
at hand johnschoeman -
If you are modeling behavior, prefer classes and composition. If
you are handling data, prefer data pipelines and folds. johnschoeman -
New Method New Data OO Existing code unchanged Existing code
changed FP Existing code changed Existing code unchanged johnschoeman -
OO Typical Cases • Resource modeling • Behaviour modeling •
State modeling johnschoeman -
FP Typical Cases • Data transformations • Data aggregates •
Data streaming johnschoeman -
Given that methods are easy to add in OO and
data is easy to add in FP johnschoeman -
Ask how you expect requirements will change before beginning a
task johnschoeman -
Base this question off the context of your business and
what users might want johnschoeman -
RSpec.describe "User uploads a file of produce data" do scenario
"by visiting the root page and clicking 'upload file'" do visit root_path attach_file("file", Rails.root.join("spec/fixtures/products.csv")) click_on "Upload File" expect(Product.count).to eq 3 end end johnschoeman -
class ImportsController < ApplicationController def create Product.import(params[:file].path) redirect_to products_path, notice:
"Succesfully imported" end end johnschoeman -
describe ".import" do context "the file is a csv" do
it "saves every row in the file as new product" do filepath = stub_csv Product.import(filepath) expect(Product.count).to eq 3 end end end johnschoeman -
class Product < ApplicationRecord validates :name, presence: true ... def
self.import(file_path) CSV.foreach(file_path, headers: true) do |row| data = row.to_h.symbolize_keys data[:title] = data[:title].titleize data[:release_date] = Time.zone.parse(data[:release_date]) create(data) end end end johnschoeman -
- spec - fixtures - products.csv - features - user_uploads_a_products_file_spec.rb
- models - product_spec.rb johnschoeman -
Commit 1 Allow users to upload csv of products Introduce
product data importer johnschoeman -
RSpec.describe ProductDataImporter it "saves every row in the file as
new product" do filepath = stub_csv importer = ProductDataImporter.new(filepath) importer.import expect(Product.count).to eq 3 end end johnschoeman -
class ProductDataImporter attr_reader :filepath def initialize(filepath) @filepath = filepath end
def import CSV.foreach(filepath, headers: true) do |row| data = row.to_h.symbolize_keys data[:title] = data[:title].titleize data[:release_date] = Time.zone.parse(data[:release_date]) Product.create(data) end end end johnschoeman -
class ImportsController < ApplicationController def create importer = ProductDataImporter.new(params[:file].path) importer.import
redirect_to products_path, notice: "Succesfully imported" end end johnschoeman -
Commit 2 Allow users to upload csv of products Introduce
product data importer Introduce product data formatter johnschoeman -
Rspec.describe ProductDataFormatter it "builds a data hash from a csv_row
for the product" do headers = %w[name author version release_date value active] fields = %w[name_a author_a 1.10.3 20190101 100 true] csv_row = CSV::Row.new(headers, fields) formatter = ProductDataFormatter.new result = formatter.build(csv_row) expect(result).to eq(expected_results) end end johnschoeman -
class ProductDataFormatter def build(csv_row) data = csv_row.to_h.symbolize_keys data[:title] = data[:title].titleize
data[:release_date] = Time.zone.parse(data[:release_date]) data end end johnschoeman -
Commit 3 Allow users to upload csv of products Introduce
product data importer Introduce product data formatter Allow .xlsx format for importer johnschoeman -
Rspec.describe ProductDataImporter context "the file is a xlsx" do it
"saves every row in the file as new product" do filename = "products.xlsx" stub_xlsx(filename) formatter = ProductDataFormatter.new importer = ProductDataImporter.new(filename, formatter) importer.import expect(Product.count).to eq 3 end end end johnschoeman -
class ProductDataImporter attr_reader :filepath, :formatter def initialize(filepath, formatter) @filepath =
filepath @formatter = formatter end def import case File.extname(filepath) when ".csv" import_csv when ".xlsx" import_xlsx end end private def import_csv CSV.foreach(filepath, headers: true) do |row| formatted_data = formatter.build(row) formatted_data = formatter.build_csv(row) Product.create(formatted_data) end end def import_xlsx Xlsx.foreach(filepath) do |row| formatted_data = formatter.build_xlsx(row) Product.create(formatted_data) end end ... end johnschoeman -
Commit 4 Allow users to upload csv of products Introduce
product data importer Introduce product data formatter Allow .xlsx format for importer Refactor importer johnschoeman -
RSpec.describe CsvImporter describe "#import" do it "saves every row in
the file as new product" do filename = "products.csv" stub_csv(filename) formatter = ProductDataFormatter.new importer = CsvImporter.new(filename, formatter) importer.import expect(Product.count).to eq 3 end end end johnschoeman -
class CsvImporter attr_reader :filepath, :formatter def initialize(filepath, formatter) @filepath =
filepath @formatter = formatter end def import CSV.foreach(filepath, headers: true) do |row| formatted_data = formatter.build_csv(row) Product.create(formatted_data) end end end johnschoeman -
RSpec.describe XlsxImporter describe "#import" do it "saves every row in
the file as new product" do filename = "products.xlsx" stub_xlsx(filename) formatter = ProductDataFormatter.new importer = XlsxImporter.new(filename, formatter) importer.import expect(Product.count).to eq 3 end end end johnschoeman -
class XlsxImporter attr_reader :filepath, :formatter def initialize(filepath, formatter) @filepath =
filepath @formatter = formatter end def import Xlsx.foreach(filepath) do |row| formatted_data = formatter.build_xlsx(row) Product.create(formatted_data) end end end johnschoeman -
Commit 5 Allow users to upload csv of products Introduce
product data importer Introduce product data formatter Allow .xlsx format for importer Refactor importer Introduce file importer johnschoeman -
RSpec.describe FileImporter describe "#import" do it "raise if called" do
importer = FileImporter.new("filepath", double("formatter")) expect { importer.import }.to raise_error("Must overwrite method") end end end johnschoeman -
class FileImporter attr_reader :filepath, :formatter def initialize(filepath, formatter) @filepath =
filepath @formatter = formatter end def import raise "Must overwrite method" end end johnschoeman -
class XlsxImporter < FileImporter ... end class CsvImporter < FileImporter
... end johnschoeman -
Commit 6 Allow users to upload csv of products Introduce
product data importer Introduce product data formatter Allow .xlsx format for importer Refactor importer Introduce file importer Introduce data builder class johnschoeman -
RSpec.describe ProductDataBuilder describe "#build" do it "raises" do formatter =
double builder = ProductDataBuilder.new(formatter) expect { builder.build }.to raise_error("Must override method") end end end johnschoeman -
class ProductDataBuilder attr_reader :formatter def initialize(formatter) @formatter = formatter end
def build raise "Must override method" end end johnschoeman -
RSpec.describe CsvBuilder do describe "#build" do context "when given a
csv_row of product data" do it "creates a hash of the product data" do headers = %w[name author version release_date value active] fields = %w[name_a author_a 1.10.3 20190101 100 true] csv_row = CSV::Row.new(headers, fields) formatter = ProductDataFormatter.new builder = CsvBuilder.new(formatter) result = builder.build(csv_row) expect(result).to eq({ ...data }) end end end end johnschoeman -
RSpec.describe XlsxBuilder do describe "#build" do context "when given a
xlsx_row of product data" do it "creates a hash of the product data" do headers = %w[name author version release_date value active] fields = %w[name_a author_a 1.10.3 20190101 100 true] xslx_row = stub_xslx_row(headers, fields) formatter = ProductDataFormatter.new builder = CsvBuilder.new(formatter) result = builder.build(xlsx_row) expect(result).to eq({ ...data }) end end end end johnschoeman -
class XlsxBuilder < ProductDataBuilder HEADERS = %w[name author release_date value
version active].freeze def build(xlsx_row) cells = xlsx_row.cells.map(&:value) data = HEADERS.zip(cells).to_h.symbolize_keys formatter.format(data) end end johnschoeman -
Commit 7 Allow users to upload csv of products Introduce
product data importer Introduce product data formatter Allow .xlsx format for importer Refactor importer Introduce file importer Introduce data builder class Format currency data from csv johnschoeman -
RSpec.describe ProductDataFormatter do ... context "when the currency has a
dollar sign" do it "strips the dollar sign" do data = { value: "$1230" } formatter = ProductDataFormatter.new result = formatter.format(data) expect(result).to eq({ value: 1230 }) end end johnschoeman -
class ProductDataFormatter def format(data) data[:title] = format_title(data[:title]) data[:release_date] = format_date(data[:release_date])
data[:value] = format_currency(data[:value]) data end ... def format_currency(input) input.to_s.gsub(/^\$/, "").to_i end end johnschoeman -
- app - models - application_record.rb - csv_builder.rb - csv_importer.rb
- file_importer.rb - product.rb - product_data_builder.rb - product_data_formatter.rb - product_data_importer.rb - xlsx_builder.rb - xlsx_importer.rb johnschoeman -
- spec - models - csv_builder_spec.rb - csv_importer_spec.rb - file_importer_spec.rb
- product_spec.rb - product_data_builder_spec.rb - product_data_formatter_spec.rb - product_data_importer_spec.rb - xlsx_builder_spec.rb - xlsx_importer_spec.rb johnschoeman -
Commit 1 Allow users to upload csv of products Introduce
product importer service johnschoeman -
RSpec.describe ProductDataImporter do describe ".import" do it "takes a file
and creates product data from the data" do filename = Rails.root.join("spec/fixtures/products.csv") importer = ProductDataImporter.new expected_data = { ...data } importer.import(filename) expect(Product.count).to eq 3 expect_product_to_match(Product.last, expected_data) end end johnschoeman -
class ProductDataImporter def self.import(filepath) new.import(filepath) end def import(filepath) CSV.foreach(filepath, headers:
true) do |row| data = row.to_h.symbolize_keys data[:title] = data[:title].titleize data[:release_date] = Time.parse(data[:release_date] Product.create(data) end end end johnschoeman -
Commit 2 Allow users to upload csv of products Introduce
product importer service Introduce data pipeline johnschoeman -
class ProductDataImporter def self.import(filepath) new.import(filepath) end def import(filepath) read_file(filepath). map
{ |data| process_data(data) }. map { |data| Product.create(data) } end private def read_file(filepath) CSV.foreach(filepath, headers: true).map do |row| row.to_h.symbolize_keys end end def process_data(data) process_date(data). then { |data| process_title(data) } end def process_date(data) new_data = data.dup new_data[:release_date] = Time.zone.parse(data[:release_date]) new_data end def process_title(data) new_data = data.dup new_data[:title] = title.titleize new_data end end johnschoeman -
class ProductDataImporter ... def import(filepath) read_file(filepath). map { |data| process_data(data)
}. map { |data| Product.create(data) } end private ... def process_data(data) process_date(data). then { |data| process_title(data) } end ... end johnschoeman -
Commit 3 Allow users to upload csv of products Introduce
product importer service Introduce data pipeline Allow for users to upload either xlsx or csv johnschoeman -
RSpec.describe ProductDataImporter do ... context "when provided a xlsx file"
do it "creates product data from the data" do filename = Rails.root.join("spec/fixtures/products.xlsx") importer = ProductDataImporter.new expected_data = { ...data } importer.import(filename) expect(Product.count).to eq 4 expect_product_to_match(Product.last, expected_data) end johnschoeman -
class ProductDataImporter ... def import(filepath) read_file(filepath). map { |data| process_data(data)
}. map { |data| Product.create(data) } end private def read_file(filepath) case File.extname(filepath) when ".csv" read_csv(filepath) when ".xlsx" read_xlsx(filepath) end end def read_xlsx(filepath) foreach_xlsx(filepath).map do |row| cells = row.cells.map(&:value) data = HEADERS.zip(cells).to_h.symbolize_keys end end ... end johnschoeman -
Commit 4 Allow users to upload csv of products Introduce
product importer service Introduce data pipeline Allow for users to upload either xlsx or csv Format currency data from csv johnschoeman -
class ProductDataImporter ... def process_data(data) process_date(data). then { |data| process_description(data)
}. then { |data| process_currency(data) } end ... def process_currency(data) new_data = data.dup new_data[:value] = value.gsub(/^\$/, "").to_i new_data end end johnschoeman -
Recap • Initial Requirement: Clients Import CSV • and then
New Requirement: .xlsx + .csv • and then New Requirement: New currency format • and then a New Requirement... johnschoeman -
Benefits of choosing the right paradigm for the task 1.Clearer
Code 2.Less Code 3.Easier to Test and Maintain 4.Higher Development Velocity johnschoeman -
- app - models - csv_builder.rb - csv_importer.rb - file_importer.rb
- product.rb - product_data_builder.rb - product_data_formatter.rb - product_data_importer.rb - xlsx_builder.rb - xlsx_importer.rb johnschoeman -
- spec - models - csv_builder_spec.rb - csv_importer_spec.rb - file_importer_spec.rb
- product_spec.rb - product_data_builder_spec.rb - product_data_formatter_spec.rb - product_data_importer_spec.rb - xlsx_builder_spec.rb - xlsx_importer_spec.rb johnschoeman -
- spec - fixtures - products.csv - products.xlsx - services
- product_data_importer_spec.rb johnschoeman -
Easier to Test / Maintain Style Public APIs added OO
8 FP 1 johnschoeman -
Higher Development Velocity Style Dev Time OO ~ A day
FP ~ An hour johnschoeman -
Paradigm Smells Using OO for a task that lends itself
to FP 1.Lots of 'Something-er' Classes 2.UML is a Linked List johnschoeman -
Lots of 'Something-er' Classes - app - models - csv_builder.rb
- csv_importer.rb - file_importer.rb - product.rb - product_data_builder.rb - product_data_formatter.rb - product_data_importer.rb - xlsx_builder.rb - xlsx_importer.rb johnschoeman -
Action Item Before beginning a task, Ask how you expect
requirements will change: Data or Behaviour? If Data, consider a functional style If Behavior, consider a object oriented style johnschoeman -
Thesis Ruby is a multi-paradigm language. Different paradigms lend themselves
to different tasks We should choose our style based off of our task johnschoeman -
FP Learning Resources Gary Bernhardt - Functional Core, Imperative Shell
thoughtbot.com/blog Piotr Solnica - Blending Functional and OO Programming in Ruby johnschoeman