Class: Codestrap::Object::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/codestrap/object/factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Factory

Returns a new instance of Factory.



12
13
14
15
# File 'lib/codestrap/object/factory.rb', line 12

def initialize(*args)
  @namespace = args.shift.capitalize if args.length > 0
  @namespace ||= 'Standard'
end

Instance Attribute Details

#cliCodestrap::CLI

CLI object

Returns:

  • (Codestrap::CLI)


25
26
27
# File 'lib/codestrap/object/factory.rb', line 25

def cli
  @cli
end

#clientsArray<Codestrap::Client>

Array of client objects

Returns:



35
36
37
# File 'lib/codestrap/object/factory.rb', line 35

def clients
  @clients
end

#configCodestrap::Config

Configuration object

Returns:



30
31
32
# File 'lib/codestrap/object/factory.rb', line 30

def config
  @config
end

#dirsCodestrap::Config

Directories where to find static object files

Returns:



20
21
22
# File 'lib/codestrap/object/factory.rb', line 20

def dirs
  @dirs
end

#filesCodestrap::Config

Directories where to find static object files

Returns:



20
# File 'lib/codestrap/object/factory.rb', line 20

attr_accessor :dirs

Instance Method Details

#objectsHash

Generate a hash of key, value pair If value is of type Hash it is converted into an [OpenStruct] object

Returns:

  • (Hash)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/codestrap/object/factory.rb', line 41

def objects
  objects = {}
  scan.each_pair do |key, value|
    case value
      when Hash
        objects[key] = OpenStruct.new(value)
      else
        objects[key] = value
    end
  end
  objects
end

#scanHash

Scan for objects

Returns:

  • (Hash)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/codestrap/object/factory.rb', line 57

def scan
  objects = {}
  scan_class = "Codestrap::Object::#{@namespace.to_s}".split('::').inject(Object) { |o, c| o.const_get c }
  klasses = scan_class.constants.map do |constant|
    "Codestrap::Object::Standard::#{constant.to_s}".split('::').inject(Object) { |o, c| o.const_get c }
  end
  Array(klasses).sort { |a, b| a.weight <=> b.weight }.each do |klass|
    klass.dirs    = @dirs
    klass.cli     = @cli
    klass.config  = @config
    klass.clients = @clients
    klass.objects.each_pair do |key, value|
      objects[key.downcase] = value
    end
  end
  objects
end

#to_hashObject



75
76
77
78
79
80
81
82
83
# File 'lib/codestrap/object/factory.rb', line 75

def to_hash
  objects = {}
  scan.each_pair do |key, value|
    if value.is_a?(Hash)
      objects[key] = value
    end
  end
  objects
end