Class: Upmin::Association
- Inherits:
-
Object
- Object
- Upmin::Association
- Defined in:
- lib/upmin/association.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #collection? ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(model, assoc_name, options = {}) ⇒ Association
constructor
A new instance of Association.
- #title ⇒ Object
- #type ⇒ Object
- #upmin_values(options = {}) ⇒ Object
- #value ⇒ Object
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, = {}) 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
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/upmin/association.rb', line 3 def model @model end |
#name ⇒ Object (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
43 44 45 |
# File 'lib/upmin/association.rb', line 43 def collection? raise NotImplementedError end |
#empty? ⇒ 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 |
#title ⇒ Object
24 25 26 |
# File 'lib/upmin/association.rb', line 24 def title return name.to_s.humanize end |
#type ⇒ Object
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( = {}) [:limit] ||= 5 if collection? vals = [value.limit([:limit])].flatten else vals = [value] end return vals.map(&:upmin_model) end |
#value ⇒ Object
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 |