Class: Impromptu::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/impromptu/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, name) ⇒ Component

Create a new component. base_path is the ‘current working directory’ of the component, and all folder paths will be joined with this path to create an absolute path. Name is the name of the component and is currently only used as a reference. Names must be unique amongst all components, so to avoid clashes a namespacing scheme should be used.



12
13
14
15
16
17
18
19
20
# File 'lib/impromptu/component.rb', line 12

def initialize(base_path, name)
  @base_path    = base_path || Pathname.new('.').realpath
  @name         = name
  @requirements = OrderedSet.new
  @folders      = OrderedSet.new
  @namespace    = nil
  @frozen       = false
  @dependencies_loaded = false
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



3
4
5
# File 'lib/impromptu/component.rb', line 3

def base_path
  @base_path
end

#foldersObject

Returns the value of attribute folders.



3
4
5
# File 'lib/impromptu/component.rb', line 3

def folders
  @folders
end

#frozenObject

Returns the value of attribute frozen.



3
4
5
# File 'lib/impromptu/component.rb', line 3

def frozen
  @frozen
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/impromptu/component.rb', line 3

def name
  @name
end

#namespace(name = nil) ⇒ Object

Define a namespace used for all resources provided by this component. This becomes the root namespace for all top level folders. e.g if you declare a namespace ‘:Root’, and a single folder ‘src’ which contains a file ‘klass.rb’, klass.rb will declare the resource Root::Klass. By default, nested folders will extend the namespace with the name of the folder. For instance, if the src folder contained another called ‘plugins’ which contained a file ‘testing.rb’, the resource Root::Plugins::Testing would be defined. Folder declarations can override this behaviour.



67
68
69
70
71
72
73
# File 'lib/impromptu/component.rb', line 67

def namespace(name=nil)
  unless name.nil?
    protect_from_modification
    @namespace = name.to_sym
  end
  @namespace
end

#requirementsObject

Returns the value of attribute requirements.



3
4
5
# File 'lib/impromptu/component.rb', line 3

def requirements
  @requirements
end

Instance Method Details

#eql?(other) ⇒ Boolean

Override eql? so two components with the same name will be considered equal by ordered set.

Returns:

  • (Boolean)


24
25
26
# File 'lib/impromptu/component.rb', line 24

def eql?(other)
  other.name == @name
end

#folder(path, options = {}, &block) ⇒ Object

Declare a folder implementing this component. All source files within this folder are assumed to define the resources of this component. Sub-folders by default provide nested namespaces, and when used in combination with the namespace method can produce multi-level namespaces. For example, a root folder ‘src’ which contains a sub-folder ‘plugins’, and a file ‘testing.rb’ would provide the resource Plugins::Testing. If namespace was used to define a root namespace for the component, that namespace would precede the Plugins namespace. To turn off this behaviour set the nested_namespaces option to false. e.g: folder ‘src’, nested_namespaces: false



52
53
54
55
# File 'lib/impromptu/component.rb', line 52

def folder(path, options={}, &block)
  protect_from_modification
  folder = @folders << Folder.new(@base_path.join(*path), self, options, block)
end

#freezeObject

Mark a component as ‘frozen’. Modification of the component requirements or list of folders is not allowed after this.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/impromptu/component.rb', line 98

def freeze
  # create a blank namespace module if required
  unless namespace.nil?
    Impromptu.root_resource.get_or_create_child(namespace).namespace!
  end
  
  # freeze files
  @folders.each do |folder|
    folder.create_namespace
    folder.files.each do |file|
      file.freeze
    end
  end
  
  @frozen = true
end

#frozen?Boolean

True if the component definition has been frozen.

Returns:

  • (Boolean)


116
117
118
# File 'lib/impromptu/component.rb', line 116

def frozen?
  @frozen
end

#hashObject

Override hash so two components with the same name will result in the same hash value.



30
31
32
# File 'lib/impromptu/component.rb', line 30

def hash
  @name.hash
end

#load_external_dependenciesObject

Load the external dependencies required by this component. If the require fails, ruby gems is loaded and the require attempted again. Any failures after this point will cause a LoadError exception to bubble through your application.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/impromptu/component.rb', line 79

def load_external_dependencies
  return false if @dependencies_loaded
  @requirements.each do |requirement|
    begin
      require requirement
    rescue LoadError => unavailable
      begin
        require 'rubygems'
      rescue LoadError
        raise unavailable
      end
      require requirement
    end
  end
  @dependencies_loaded = true
end

#requires(*resources) ⇒ Object

Add external dependencies (such as gems) to this component. e.g: requires ‘gem_name’, ‘other_file’. May be called multiple times.



36
37
38
39
# File 'lib/impromptu/component.rb', line 36

def requires(*resources)
  protect_from_modification
  @requirements.merge(resources)
end