Skip to content

Instantly share code, notes, and snippets.

@solnic
Created June 3, 2021 21:03
Show Gist options
  • Save solnic/1d8c563cd143c758d81a6c6dbd1756ed to your computer and use it in GitHub Desktop.
Save solnic/1d8c563cd143c758d81a6c6dbd1756ed to your computer and use it in GitHub Desktop.
Bare bone, no-library, no-error handling, just the very core idea behind FP objects in Ruby
class CreateStuff
def initialize(stuff_repo)
@stuff_repo = stuff_repo
end
def call(params)
@stuff_repo.create(params)
end
end
class BuildEverything
def initialize(create_stuff)
@create_stuff = create_stuff
end
def call(params)
1000.times { |i| @create_stuff.call(params.merge(num: i, foo: "foo-#{i}")) }
end
end
create_stuff = CreateStuff.new(stuff_repo) # let's assume we got stuff_repo already
build_everything = BuildEverything.new(create_stuff)
# so now I have access to creating stuff however I want
create_stuff.call(num: 312, foo: "bar")
# and some higher-level concept built on top
build_everything.call(baz: "xyz")
# this means we have stand-alone, re-usable objects, that we can pass around, compose freely together and so on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment