Class: Catche::Tag::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/catche/tag/object.rb

Constant Summary collapse

@@objects =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  @model = model
  @object = object

  association = options[: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(options)

  @associations = @options[:associations]
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



51
52
53
# File 'lib/catche/tag/object.rb', line 51

def associations
  @associations
end

#modelObject (readonly)

Returns the value of attribute model.



50
51
52
# File 'lib/catche/tag/object.rb', line 50

def model
  @model
end

#objectObject (readonly)

Returns the value of attribute object.



50
51
52
# File 'lib/catche/tag/object.rb', line 50

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



50
51
52
# File 'lib/catche/tag/object.rb', line 50

def options
  @options
end

Class Method Details

.clearObject



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, options={})
  new_object = self.new(model, object, options)

  objects.each do |tag_object|
    return tag_object if tag_object.same?(new_object)
  end

  new_object
end

.for(model, object, options = {}) ⇒ Object

Returns an existing (duplicate) or new tag object for the given arguments.

Catche::Tag::Object.for(Project, ProjectsController)


12
13
14
15
16
17
18
19
# File 'lib/catche/tag/object.rb', line 12

def for(model, object, options={})
  tag_object = find_or_initialize(model, object, options)

  objects << tag_object
  objects.uniq!

  tag_object
end

.objectsObject



44
45
46
# File 'lib/catche/tag/object.rb', line 44

def objects
  @@objects
end

Instance Method Details

#association_tags(initialized_object) ⇒ Object

Maps association tags.

object = Catche::Tag::Object.new(Task, TasksController, :through => [:user, :project])
object.association_tags(controller) => ['users_1', 'projects_1']


120
121
122
# File 'lib/catche/tag/object.rb', line 120

def association_tags(initialized_object)
  resource_tags(*Catche::Tag::Resource.associations(initialized_object, associations))
end

#bubble?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/catche/tag/object.rb', line 124

def bubble?
  options[: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 expiration_tags(initialized_object)
  tags = []

  # Add collection tags when enabled
  tags << Catche::Tag.join(
      association_tags(initialized_object),
      options[:collection_name]
    ) if options[:expire_collection]

  tags << Catche::Tag.join(
      association_tags(initialized_object),
      identifier_tags(initialized_object)
    )

  tags.uniq
end

#identifier_tags(initialized_object) ⇒ Object

Identifying tag for the current resource or collection.

object = Catche::Tag::Object.new(Task, TasksController)
object.identifier_tags(controller) => ['tasks', 1]


107
108
109
# File 'lib/catche/tag/object.rb', line 107

def identifier_tags(initialized_object)
  Catche::Tag.join options[:collection_name], Catche::Tag::Resource.resource(initialized_object, options[: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 resource_tags(*resources)
  resources.map { |resource| Catche::Tag.join(Catche::Tag::Resource.pluralize(resource.class), resource.id) }
end

#same?(object) ⇒ Boolean

Returns:

  • (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.options    == object.options
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.tags(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 tags(initialized_object)
  tags = []

  tags += association_tags(initialized_object) if bubble?
  tags += expiration_tags(initialized_object)

  tags
end