Sha256: d27749d63dea5545e613afd021ffaaa395ec27ed63e51a489e50ad21482e3db3

Contents?: true

Size: 927 Bytes

Versions: 15

Compression:

Stored size: 927 Bytes

Contents

module SuperValue
  class Base
    include ActiveSupport::Callbacks
    cattr_accessor :model_name
    define_callbacks :init

    class << self
      def init(model_name)
        self.model_name = model_name
        self.send(:attr_accessor, model_name)
        define_method :initialize do |model|
          run_callbacks :init do
            self.send("#{model_name}=", model)
          end
        end
      end

      def property(method_name)
        delegate method_name, to: model_name
      end

      def before_init(method_name=nil, &block)
        if block_given?
          set_callback :init, :before, &block
        else
          set_callback :init, :before, method_name
        end
      end

      def after_init(method_name=nil, &block)
        if block_given?
          set_callback :init, :after, &block
        else
          set_callback :init, :after, method_name
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
super_tools-3.0.1 lib/super_value/base.rb
super_tools-2.1.0 lib/super_value/base.rb
super_tools-0.0.26 lib/super_value/base.rb
super_tools-0.0.25 lib/super_value/base.rb
super_tools-0.0.21 lib/super_value/base.rb
super_tools-0.0.20 lib/super_value/base.rb
super_tools-0.0.17 lib/super_value/base.rb
super_tools-0.0.15 lib/super_value/base.rb
super_tools-0.0.14 lib/super_value/base.rb
super_tools-0.0.13 lib/super_value/base.rb
super_tools-0.0.12 lib/super_value/base.rb
super_tools-0.0.11 lib/super_value/base.rb
super_tools-0.0.10 lib/super_value/base.rb
super_tools-0.0.9 lib/super_value/base.rb
super_tools-0.0.8 lib/super_value/base.rb