Class: Catche::Tag::Object
- Inherits:
-
Object
- Object
- Catche::Tag::Object
- Defined in:
- lib/catche/tag/object.rb
Constant Summary collapse
- @@objects =
[]
Instance Attribute Summary collapse
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .clear ⇒ Object
- .find_by_association(association) ⇒ Object
- .find_by_model(model) ⇒ Object
-
.find_or_initialize(model, object, options = {}) ⇒ Object
Finds a previously declared (same) tag object or returns a new one.
-
.for(model, object, options = {}) ⇒ Object
Returns an existing (duplicate) or new tag object for the given arguments.
- .objects ⇒ Object
Instance Method Summary collapse
-
#association_tags(initialized_object) ⇒ Object
Maps association tags.
- #bubble? ⇒ Boolean
-
#expiration_tags(initialized_object) ⇒ Object
The tags that should expire as soon as the resource or collection changes.
-
#identifier_tags(initialized_object) ⇒ Object
Identifying tag for the current resource or collection.
-
#initialize(model, object, options = {}) ⇒ Object
constructor
A new instance of Object.
-
#resource_tags(*resources) ⇒ Object
Maps the given resources names to tags by fetching the resources from the given object.
- #same?(object) ⇒ Boolean
-
#tags(initialized_object) ⇒ Object
Returns the tags for the given object.
Constructor Details
#initialize(model, object, options = {}) ⇒ Object
Returns a new instance of Object.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/catche/tag/object.rb', line 53 def initialize(model, object, ={}) @model = model @object = object association = [:through] @options = { :resource_name => Catche::Tag::Resource.singularize(@model), :collection_name => Catche::Tag::Resource.pluralize(@model), :associations => [association].flatten.compact, :bubble => false, :expire_collection => true }.merge() @associations = @options[:associations] end |
Instance Attribute Details
#associations ⇒ Object
Returns the value of attribute associations.
51 52 53 |
# File 'lib/catche/tag/object.rb', line 51 def associations @associations end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
50 51 52 |
# File 'lib/catche/tag/object.rb', line 50 def model @model end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
50 51 52 |
# File 'lib/catche/tag/object.rb', line 50 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
50 51 52 |
# File 'lib/catche/tag/object.rb', line 50 def @options end |
Class Method Details
.clear ⇒ Object
40 41 42 |
# File 'lib/catche/tag/object.rb', line 40 def clear @@objects = [] end |
.find_by_association(association) ⇒ Object
36 37 38 |
# File 'lib/catche/tag/object.rb', line 36 def find_by_association(association) objects.select { |tag_object| tag_object.associations.include?(association) } end |
.find_by_model(model) ⇒ Object
32 33 34 |
# File 'lib/catche/tag/object.rb', line 32 def find_by_model(model) objects.select { |tag_object| tag_object.model == model } end |
.find_or_initialize(model, object, options = {}) ⇒ Object
Finds a previously declared (same) tag object or returns a new one.
22 23 24 25 26 27 28 29 30 |
# File 'lib/catche/tag/object.rb', line 22 def find_or_initialize(model, object, ={}) new_object = self.new(model, object, ) objects.each do |tag_object| return tag_object if tag_object.same?(new_object) end new_object end |
.for(model, object, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/catche/tag/object.rb', line 12 def for(model, object, ={}) tag_object = find_or_initialize(model, object, ) objects << tag_object objects.uniq! tag_object end |
.objects ⇒ Object
44 45 46 |
# File 'lib/catche/tag/object.rb', line 44 def objects @@objects end |
Instance Method Details
#association_tags(initialized_object) ⇒ Object
120 121 122 |
# File 'lib/catche/tag/object.rb', line 120 def (initialized_object) (*Catche::Tag::Resource.associations(initialized_object, associations)) end |
#bubble? ⇒ Boolean
124 125 126 |
# File 'lib/catche/tag/object.rb', line 124 def bubble? [:bubble] end |
#expiration_tags(initialized_object) ⇒ Object
The tags that should expire as soon as the resource or collection changes.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/catche/tag/object.rb', line 86 def (initialized_object) = [] # Add collection tags when enabled << Catche::Tag.join( (initialized_object), [:collection_name] ) if [:expire_collection] << Catche::Tag.join( (initialized_object), (initialized_object) ) .uniq end |
#identifier_tags(initialized_object) ⇒ Object
107 108 109 |
# File 'lib/catche/tag/object.rb', line 107 def (initialized_object) Catche::Tag.join [:collection_name], Catche::Tag::Resource.resource(initialized_object, [:resource_name]).try(:id) end |
#resource_tags(*resources) ⇒ Object
Maps the given resources names to tags by fetching the resources from the given object.
112 113 114 |
# File 'lib/catche/tag/object.rb', line 112 def (*resources) resources.map { |resource| Catche::Tag.join(Catche::Tag::Resource.pluralize(resource.class), resource.id) } end |
#same?(object) ⇒ Boolean
128 129 130 131 132 |
# File 'lib/catche/tag/object.rb', line 128 def same?(object) self.model == object.model && self.object == object.object && self. == object. end |
#tags(initialized_object) ⇒ Object
Returns the tags for the given object. If ‘bubble` is set to true it will pass along the separate tag for each association. This means the collection or resource is expired as soon as the association changes.
object = Catche::Tag::Object.new(Task, TasksController, :through => [:user, :project])
object.(controller.new) => ['users_1_projects_1_tasks_1']
76 77 78 79 80 81 82 83 |
# File 'lib/catche/tag/object.rb', line 76 def (initialized_object) = [] += (initialized_object) if bubble? += (initialized_object) end |