Class: JIRA::HasManyProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/jira/has_many_proxy.rb

Overview

Whenever a collection from a has_many relationship is accessed, an instance of this class is returned. This instance wraps the Array of instances in the collection with an extra build method, which allows new instances to be built on the collection with the correct properties.

In practice, instances of this class behave exactly like an Array.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, target_class, collection = []) ⇒ HasManyProxy

Returns a new instance of HasManyProxy.


13
14
15
16
17
# File 'lib/jira/has_many_proxy.rb', line 13

def initialize(parent, target_class, collection = [])
  @parent       = parent
  @target_class = target_class
  @collection   = collection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Delegate any missing methods to the collection that this proxy wraps


39
40
41
# File 'lib/jira/has_many_proxy.rb', line 39

def method_missing(method_name, *args, &block)
  collection.send(method_name, *args, &block)
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection


11
12
13
# File 'lib/jira/has_many_proxy.rb', line 11

def collection
  @collection
end

#parentObject (readonly)

Returns the value of attribute parent


10
11
12
# File 'lib/jira/has_many_proxy.rb', line 10

def parent
  @parent
end

#target_classObject (readonly)

Returns the value of attribute target_class


10
11
12
# File 'lib/jira/has_many_proxy.rb', line 10

def target_class
  @target_class
end

Instance Method Details

#allObject

Forces an HTTP request to fetch all instances of the target class that are associated with the parent


34
35
36
# File 'lib/jira/has_many_proxy.rb', line 34

def all
  target_class.all(parent.client, parent.to_sym => parent)
end

#build(attrs = {}) ⇒ Object

Builds an instance of this class with the correct parent. For example, issue.comments.build(attrs) will initialize a comment as follows:

JIRA::Resource::Comment.new(issue.client,
                            :attrs => attrs,
                            :issue => issue)

26
27
28
29
30
# File 'lib/jira/has_many_proxy.rb', line 26

def build(attrs = {})
  resource = target_class.new(parent.client, :attrs => attrs, parent.to_sym => parent)
  collection << resource
  resource
end