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.



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

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/jira/base_factory.rb', line 4

def client
  @client
end

Class Method Details

.delegate_to_target_class(*method_names) ⇒ Object



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

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



42
43
44
# File 'lib/jira/base_factory.rb', line 42

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.



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

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 components.  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