Sha256: 764ac3a122bd8e75542438f4961501bc4f0a849ac5533d5e69e8b3fde8a88c16

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module Motion
  module Persistable
    
    def self.included(base)
      base.extend ClassMethods
    end

    def persistence_key
      if respond_to?(:key)
        key
      else
        name
      end
    end
  
    module ClassMethods
  
      def attr_persisted(method_name, default_value = nil, key_suffix = nil, &block)
        callback = block if block_given?

        instance_eval do
              
          define_method method_name do
            if App::Persistence[send("_#{method_name}_key")].nil? and default_value
              App::Persistence[send("_#{method_name}_key")] = default_value
            end          
            App::Persistence[send("_#{method_name}_key")]
          end
      
          define_method "#{method_name}=" do |value|
            App::Persistence[send("_#{method_name}_key")] = value
            callback.call(value) if callback
            App::Persistence[send("_#{method_name}_key")]
          end
      
          define_method "_#{method_name}_key" do
            key_suffix = send(key_suffix) if key_suffix.is_a?(Symbol)
            [persistence_key, key_suffix, method_name].compact.join('.')
          end
      
          private "_#{method_name}_key"
      
        end
      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
motion-persistable-0.0.3 lib/motion/persistable.rb
motion-persistable-0.0.2 lib/motion/persistable.rb