Sha256: f5b6e85a132d9387153e70d8927707e6a9c4087427bc452dfce1800b1fc730a4

Contents?: true

Size: 1.87 KB

Versions: 11

Compression:

Stored size: 1.87 KB

Contents

# SmartIoC::BeanLocations is a storage for locations of package bean definitions.
# Storage structure:
# {
#   PACKAGE_NAME => { BEAN_SYM => BEAN_PATH}
# }
# Ex:
# {
#   repository: {
#     users_repository: ['/app/core/infrastructure/repository/users.rb'],
#     posts_repository: ['/app/core/infrastructure/repository/posts.rb']
#   }
# }
class SmartIoC::BeanLocations
  @data = {}

  class << self
    # @param package_name [Symbol] bean package name (ex: :repository)
    # @param bean [Symbol] bean name (ex: :users_repository)
    # @param bean_path [String] bean name (ex: :users_repository)
    # @return nil
    # @raise [ArgumentError] if bean previous bean definition with same name was found for package
    def add_bean(package_name, bean, bean_path)
      @data[package_name] ||= {}
      package_beans = @data[package_name]

      package_beans[bean] ||= []
      package_beans[bean].push(bean_path)

      nil
    end

    # @param bean [Symbol] bean name (ex: :users_repository)
    # @return Hash[Array[String]] hash of bean definitions from all packages
    def get_bean_locations(bean)
      locations = {}

      @data.each do |package, bean_locations|
        if bean_locations.has_key?(bean)
          locations[package] ||= []
          locations[package] += bean_locations[bean]
        end
      end

      locations
    end

    def load_all
      @data.each do |package, bean_locations|
        bean_locations.each do |bean, paths|
          paths.each do |path|
            load(path)
          end
        end
      end
    end

    def clear
      @data = {}
    end

    # @param path [String] absolute bean path
    # @return [nil or String] package name be absolute bean path
    def get_bean_package(path)
      @data.each do |package, bean_locations|
        if bean_locations.values.flatten.include?(path)
          return package
        end
      end

      nil
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
smart_ioc-0.1.30 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.29 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.28 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.27 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.26 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.25 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.24 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.23 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.22 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.21 lib/smart_ioc/bean_locations.rb
smart_ioc-0.1.20 lib/smart_ioc/bean_locations.rb