Sha256: d15df3b9f205f35f0169132c48dead7d8ad5d50d14b0c56ede7d6a7adcc2e50e

Contents?: true

Size: 440 Bytes

Versions: 4

Compression:

Stored size: 440 Bytes

Contents

module AutoBuild
  class HasOneHook
    attr_reader :model, :association_name

    def initialize(model, name)
      @model = model
      @association_name = name
    end

    def attach
      hook_code = code
      model.class_eval do
        after_initialize do |record|
          record.instance_eval(hook_code)
        end
      end
    end

    def code
      "self.#{association_name} ||= build_#{association_name}"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
auto_build-0.5.0 lib/auto_build/has_one_hook.rb
auto_build-0.4.3 lib/auto_build/has_one_hook.rb
auto_build-0.4.2 lib/auto_build/has_one_hook.rb
auto_build-0.4.0 lib/auto_build/has_one_hook.rb