Class: JIRA::BaseFactory

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

Overview

This is the base class for all the JIRA resource factory instances.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ BaseFactory

Returns a new instance of BaseFactory.



8
9
10
# File 'lib/jira/base_factory.rb', line 8

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/jira/base_factory.rb', line 6

def client
  @client
end

Class Method Details

.delegate_to_target_class(*method_names) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/jira/base_factory.rb', line 30

def self.delegate_to_target_class(*method_names)
  method_names.each do |method_name|
    define_method method_name do |*args|
      target_class.send(method_name, @client, *args)
    end
  end
end

Instance Method Details

#build(attrs = {}) ⇒ Object

This method needs special handling as it has a default argument value



44
45
46
# File 'lib/jira/base_factory.rb', line 44

def build(attrs={})
  target_class.build(@client, attrs)
end

#target_classObject

Return the name of the class which this factory generates, i.e. JIRA::Resource::FooFactory creates JIRA::Resource::Foo instances.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jira/base_factory.rb', line 14

def target_class
  # Need to do a little bit of work here as Module.const_get doesn't work
  # with nested class names, i.e. JIRA::Resource::Foo.
  #
  # So create a method chain from the class componenets.  This code will
  # unroll to:
  #   Module.const_get('JIRA').const_get('Resource').const_get('Foo')
  #
  target_class_name = self.class.name.sub(/Factory$/, '')
  class_components = target_class_name.split('::')

  class_components.inject(Module) do |mod, const_name|
    mod.const_get(const_name)
  end
end