davydovanton / kan

Simple, functional authorization library and role management for ruby
http://www.kanrb.org
MIT License
232 stars 12 forks source link

Application with default settings #6

Open davydovanton opened 6 years ago

davydovanton commented 6 years ago

Now we have a simple problem: how to set up default values for each abilities instance in one place?

I sugest to use something like this:

class ProjectAbilityApplication < Kan::Application
  default_options logger: MyLogger.new
end

After that we can initialize:

Kan::Application.new(
  comment: Comments::Abilities.new
) # => will use default Logger
ProjectAbilityApplication.new(
  comment: Comments::Abilities.new
) # => will use default MyLogger
ProjectAbilityApplication.new(
  comment: Comments::Abilities.new(logger: MyOtherLogger.new)
) # => will use MyOtherLogger

WDYT?


UPD agter small talk with @apotonick: Maybe we need to use instance variable for settings and #call for generating a builder class 🤔

Kan::Application.new.call(
  comment: Comments::Abilities.new
) # => will use default Logger

Kan::Application.new(logger: MyLogger.new).call(
  comment: Comments::Abilities.new
) # will use MyLogger

Kan::Application.new(logger: MyLogger.new).call(
  comment: Comments::Abilities.new(logger: MyOtherLogger.new)
) # => will use MyOtherLogger
apotonick commented 6 years ago

Hey, I like the third step where you can override it as an argument, but I don't understand how the ProjectAbilityApplication manages to set a MyLogger on the Comments ability object. Are you using a writer internally for that? Because that I think I wouldn't like.