Class: Ripple::Association

Inherits:
Object show all
Includes:
Translation
Defined in:
ripple/lib/ripple/associations.rb

Overview

The “reflection” for an association - metadata about how it is configured.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Translation

#i18n_scope

Methods included from Riak::Util::Translation

#i18n_scope, #t

Constructor Details

- (Association) initialize(type, name, options = {})

association options :using, :class_name, :class, :extend, options that may be added :validate



152
153
154
# File 'ripple/lib/ripple/associations.rb', line 152

def initialize(type, name, options={})
  @type, @name, @options = type, name, options.to_options
end

Instance Attribute Details

- (Object) name (readonly)

Returns the value of attribute name



147
148
149
# File 'ripple/lib/ripple/associations.rb', line 147

def name
  @name
end

- (Object) options (readonly)

Returns the value of attribute options



147
148
149
# File 'ripple/lib/ripple/associations.rb', line 147

def options
  @options
end

- (Object) type (readonly)

Returns the value of attribute type



147
148
149
# File 'ripple/lib/ripple/associations.rb', line 147

def type
  @type
end

Instance Method Details

- (Object) class_name

String The class name of the associated object(s)

Returns:

  • String The class name of the associated object(s)



157
158
159
160
161
162
163
164
165
166
167
168
# File 'ripple/lib/ripple/associations.rb', line 157

def class_name
  @class_name ||= case
                  when @options[:class_name]
                    @options[:class_name]
                  when @options[:class]
                    @options[:class].to_s
                  when many?
                    @name.to_s.classify
                  else
                    @name.to_s.camelize
                  end
end

- (true, false) embeddable?

Is the associated class an EmbeddedDocument

Returns:

  • (true, false)

    Is the associated class an EmbeddedDocument



186
187
188
# File 'ripple/lib/ripple/associations.rb', line 186

def embeddable?
  klass.embeddable?
end

- (String) ivar

The instance variable in the owner where the association will be stored

Returns:

  • (String)

    the instance variable in the owner where the association will be stored



202
203
204
# File 'ripple/lib/ripple/associations.rb', line 202

def ivar
  "@_#{name}"
end

- (Class) klass

The class of the associated object(s)

Returns:

  • (Class)

    The class of the associated object(s)



171
172
173
# File 'ripple/lib/ripple/associations.rb', line 171

def klass
  @klass ||= options[:class] || class_name.constantize
end

A filter proc to be used with Enumerable#select for collecting links that belong to this association (only when #linked? is true)

Returns:

  • (Proc)

    a filter proc to be used with Enumerable#select for collecting links that belong to this association (only when #linked? is true)



218
219
220
# File 'ripple/lib/ripple/associations.rb', line 218

def link_filter
  linked? ? lambda {|link| link.tag == link_tag } : lambda {|_| false }
end

When #linked? is true, a specification for which links to follow to retrieve the associated documents

Returns:

  • (Riak::WalkSpec)

    when #linked? is true, a specification for which links to follow to retrieve the associated documents



228
229
230
231
232
233
234
235
236
237
# File 'ripple/lib/ripple/associations.rb', line 228

def link_spec
  # TODO: support transitive linked associations
  if linked?
    tag = name.to_s
    bucket = polymorphic? ? '_' : klass.bucket_name
    Riak::WalkSpec.new(:tag => tag, :bucket => bucket)
  else
    nil
  end
end

When #linked? is true, the tag for outgoing links

Returns:

  • (String, nil)

    when #linked? is true, the tag for outgoing links



223
224
225
# File 'ripple/lib/ripple/associations.rb', line 223

def link_tag
  linked? ? Array(link_spec).first.tag : nil
end

- (true, false) linked?

Does the association use links

Returns:

  • (true, false)

    Does the association use links



197
198
199
# File 'ripple/lib/ripple/associations.rb', line 197

def linked?
  using == :linked
end

- (true, false) many?

Is the cardinality of the association > 1

Returns:

  • (true, false)

    Is the cardinality of the association > 1



176
177
178
# File 'ripple/lib/ripple/associations.rb', line 176

def many?
  @type == :many
end

- (true, false) one?

Is the cardinality of the association == 1

Returns:

  • (true, false)

    Is the cardinality of the association == 1



181
182
183
# File 'ripple/lib/ripple/associations.rb', line 181

def one?
  @type == :one
end

- (true, false) polymorphic?

TODO: Polymorphic not supported

Returns:

  • (true, false)

    Does the association support more than one associated class



192
193
194
# File 'ripple/lib/ripple/associations.rb', line 192

def polymorphic?
  false
end

- (Class) proxy_class

The association proxy class

Returns:

  • (Class)

    the association proxy class



207
208
209
# File 'ripple/lib/ripple/associations.rb', line 207

def proxy_class
  @proxy_class ||= proxy_class_name.constantize
end

- (String) proxy_class_name

The class name of the association proxy

Returns:

  • (String)

    the class name of the association proxy



212
213
214
215
# File 'ripple/lib/ripple/associations.rb', line 212

def proxy_class_name
  klass_name = (many? ? 'Many' : 'One') + using.to_s.camelize + ('Polymorphic' if polymorphic?).to_s + 'Proxy'
  "Ripple::Associations::#{klass_name}"
end

- (Symbol) using

Which method is used for representing the association - currently only supports :embedded and :linked

Returns:

  • (Symbol)

    which method is used for representing the association - currently only supports :embedded and :linked



240
241
242
# File 'ripple/lib/ripple/associations.rb', line 240

def using
  @using ||= options[:using] || (embeddable? ? :embedded : :linked)
end

- (Object) verify_type!(value, owner)

Raises:

  • (ArgumentError)

    if the value does not match the class of the association



245
246
247
248
249
250
251
252
253
# File 'ripple/lib/ripple/associations.rb', line 245

def verify_type!(value, owner)
  unless type_matches?(value)
    raise ArgumentError.new(t('invalid_association_value',
                              :name => name,
                              :owner => owner.inspect,
                              :klass => polymorphic? ? "<polymorphic>" : klass.name,
                              :value => value.inspect))
  end
end