Skip to content

Using a custom setter

Johann edited this page Dec 1, 2018 · 1 revision

Creating your setter

Your setter just need to have an instance method named set which takes two option params named name and value.

Here, we'll consider using a setter for rails project.

#
# === Description ===
#
# Sets config variables in Rails.application.config
#
# === Usage ===
#
# @example:
#   setter = MyCustomRailsSetter.new
#   setter.set(name: :foo, value: :bar)
#
#   Rails.application.config.foo # => "bar"
#
class MyCustomRailsSetter
    #
    # === Description ===
    #
    # Sets config variables in the right place.
    #
    # === Usage ===
    #
    # @example:
    #   setter.set(name: 'my_var', value: 'foo')
    #
    # === Attributes ===
    #
    # @option [String | Symbol] :name
    # @option [Any] :value
    #
    def set(name:, value:)
        Rails.application.config.public_send("#{name}=", value)
    end
end

Register your setter

# Get your learner instance
knowledge = Knowledge::Learner.new

# And just set your setter
knowledge.setter = MyCustomRailsSetter.new
Clone this wiki locally