Module: Ripple::Document::ClassMethods

Defined in:
lib/ripple_oh_caramba.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#many_mutual(name, opts = {}) ⇒ Object



73
74
75
76
77
# 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 = {}) ⇒ Object



67
68
69
70
71
# 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 = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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 = {}) ⇒ Object



85
86
87
88
89
# 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 = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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 = {}) ⇒ Object



79
80
81
82
83
# 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) ⇒ Object

TOKENS



94
95
96
97
# 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) ⇒ Object



99
100
101
102
# File 'lib/ripple_oh_caramba.rb', line 99

def token_key!(&blk)
  token(:token, &blk)
  key_on :token
end