Class: Upmin::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/upmin/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, assoc_name, options = {}) ⇒ Association

Returns a new instance of Association.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/upmin/association.rb', line 6

def initialize(model, assoc_name, options = {})
  if model.class.active_record?
    extend Upmin::ActiveRecord::Association
  elsif model.class.data_mapper?
    extend Upmin::DataMapper::Association
  else
    raise ArgumentError.new(model)
  end

  @model = model
  @name = assoc_name.to_sym
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/upmin/association.rb', line 3

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/upmin/association.rb', line 4

def name
  @name
end

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/upmin/association.rb', line 43

def collection?
  raise NotImplementedError
end

#empty?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/upmin/association.rb', line 47

def empty?
  if collection?
    return value.count == 0
  else
    return ![value].flatten.any?
  end
end

#titleObject



24
25
26
# File 'lib/upmin/association.rb', line 24

def title
  return name.to_s.humanize
end

#typeObject

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/upmin/association.rb', line 39

def type
  raise NotImplementedError
end

#upmin_values(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/upmin/association.rb', line 28

def upmin_values(options = {})
  options[:limit] ||= 5
  if collection?
    vals = [value.limit(options[:limit])].flatten
  else
    vals = [value]
  end

  return vals.map(&:upmin_model)
end

#valueObject



19
20
21
22
# File 'lib/upmin/association.rb', line 19

def value
  # TODO(jon): Add some way to handle exceptions.
  return model.send(name)
end