module Ripple::Document::ClassMethods

Monkeys like to patch. This code is like an ugly duckling.

Public Instance Methods

many_mutual(name, opts={}) click to toggle source
# File lib/ripple_oh_caramba.rb, line 73
def many_mutual(name, opts={})
  as      = opts[:as]
  kls     = opts[:class]
  raise 'not imlemented yet'
end
one_mutual(name, opts={}) click to toggle source
# File lib/ripple_oh_caramba.rb, line 67
def one_mutual(name, opts={})
  as      = opts[:as]
  kls     = opts[:class]
  raise 'not imlemented yet'
end
reference_many(name, opts={}) click to toggle source
# File lib/ripple_oh_caramba.rb, line 33
  def reference_many(name, opts={})
    plural_name = name.to_s
    name        = plural_name.singularize
    kls         = opts[:class] ||
                name.camelize

    property :"#{name}_keys", Array, :default => proc {[]}
    before_save :"__set_#{name}_keys"

    class_eval <<-CODE, __FILE__, __LINE__

      def #{plural_name}
        kls = '#{kls}'.constantize
        @#{plural_name} ||= #{name}_keys.map do |key|
          kls.find(key)
        end.compact
      end

      def #{plural_name}=(val)
        @#{plural_name} = val
      end

      private
        def __set_#{name}_keys
          self.#{name}_keys = #{plural_name}.map do |item|
            item.is_a?('#{kls}'.constantize) ?
              item.key : raise(item.inspect + ' is not a #{kls}')
          end
        end
    CODE
  end
reference_many_mutual(name, opts={}) click to toggle source
# File lib/ripple_oh_caramba.rb, line 85
def reference_many_mutual(name, opts={})
  as      = opts[:as]
  kls     = opts[:class]
  raise 'not imlemented yet'
end
reference_one(name, opts={}) click to toggle source
# File lib/ripple_oh_caramba.rb, line 10
  def reference_one(name, opts={})
    name    = name.to_s
    kls     = opts[:class] ||
              name.camelize

    property :"#{name}_key", String

    class_eval <<-CODE, __FILE__, __LINE__
      def #{name}
        @#{name} ||= "#{kls}".constantize.find(#{name}_key)
      end

      def #{name}=(doc)
        if doc.class.name != "#{kls}"
          raise "got " + doc.class + ", expected #{kls}"
        end
        raise "must have a key" unless doc.key
        @#{name} = doc
        self.#{name}_key = doc.key
      end
    CODE
  end
reference_one_mutual(name, opts={}) click to toggle source
# File lib/ripple_oh_caramba.rb, line 79
def reference_one_mutual(name, opts={})
  as      = opts[:as]
  kls     = opts[:class]
  raise 'not imlemented yet'
end
token(name, &blk) click to toggle source

TOKENS

# File lib/ripple_oh_caramba.rb, line 94
def token(name, &blk)
  property name, String, :default => blk ||
    lambda { ActiveSupport::SecureRandom.urlsafe_base64(16) }
end
token_key!(&blk) click to toggle source
# File lib/ripple_oh_caramba.rb, line 99
def token_key!(&blk)
  token(:token, &blk)
  key_on :token
end