Skip to content

Instantly share code, notes, and snippets.

@gkop
Created October 24, 2012 19:23
Show Gist options
  • Save gkop/3948243 to your computer and use it in GitHub Desktop.
Save gkop/3948243 to your computer and use it in GitHub Desktop.
Best practice for methods used only during pre-initialization?
# This simple solution permanently adds a method on Object that
# will never be used outside this file.
def compile_asset?(path)
# ... lots of complicated logic ...
# returns boolean
end
Foo::Application.configure do
...
config.assets.precompile = [ method(:compile_asset?).to_proc ]
end
# One way to keep things clean
class <<Foo::Application
def compile_asset?(path)
# ... lots of complicated logic ...
# returns boolean
end
end
Foo::Application.configure do
...
config.assets.precompile = [ method(:compile_asset?).to_proc ]
end
class <<Foo::Application
remove_method :compile_asset?
end
# What's a more elegant way?
@rahearn
Copy link

rahearn commented Oct 24, 2012

Does compile_asset? ever use anything other than the path parameter? If not, then you could just move it inline:

Foo::Application.configure do
  ...
  config.assets.precompile = [ ->(path) {
    # lots of complicated logic ...
    # returns boolean
  } ]
end

@gkop
Copy link
Author

gkop commented Oct 24, 2012

@rahearn suppose if you will the precompile method calls two other methods that each encapsulate a bit of file-matching logic.

@rahearn
Copy link

rahearn commented Oct 24, 2012

I'm probably wrap them all in a module that sticks around but then doesn't leak out into Object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment