Module: NeedsResources

Defined in:
lib/needs_resources.rb,
lib/needs_resources/version.rb,
lib/needs_resources/resource_type.rb,
lib/needs_resources/resource_container.rb,
lib/needs_resources/top_level_resources.rb,
lib/needs_resources/nested_resource_type.rb,
lib/needs_resources/missing_resource_error.rb,
lib/needs_resources/required_attribute_error.rb,
lib/needs_resources/invalid_resource_key_error.rb,
lib/needs_resources/invalid_resource_alias_error.rb,
lib/needs_resources/invalid_resource_value_error.rb,
lib/needs_resources/invalid_or_corrupted_resources_error.rb

Defined Under Namespace

Modules: NestedResourceType, ResourceContainer, ResourceType Classes: InvalidOrCorruptedResourcesError, InvalidResourceAliasError, InvalidResourceKeyError, InvalidResourceValueError, MissingResourceError, RequiredAttributeError, TopLevelResources

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_resourcesObject



18
19
20
21
22
23
# File 'lib/needs_resources.rb', line 18

def self.ensure_resources
  if (missing = missing_resources).any?
    # TODO: maybe prompt user if missing?
    raise MissingResourceError.new(*missing)
  end
end

.missing_resourcesObject



25
26
27
# File 'lib/needs_resources.rb', line 25

def self.missing_resources
  TopLevelResources.instance.missing_resources
end

Instance Method Details

#needs_resources(*names) ⇒ Object Also known as: needs_resource



5
6
7
8
9
10
11
12
13
14
# File 'lib/needs_resources.rb', line 5

def needs_resources(*names)
  container = is_a?(ResourceContainer) ? self : TopLevelResources.instance
  names.flatten.each do |name|
    if is_a? Class
      define_method name, lambda { container[name] }
    end
    define_singleton_method name, lambda { container[name] }
    container.resources_needed << name.to_sym
  end
end