Sha256: 7f835a246c15e3080f2371d7bed24629d96ae83e87bdd245a63c6496f92436e7

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

module MixedGauge
  class SubModelRepository
    attr_reader :base_class

    # @param [ClusterConfig] cluster_config
    # @param [Class] base_class A AR Model
    def initialize(cluster_config, base_class)
      @base_class = base_class

      sub_models = cluster_config.connections.map do |connection_name|
        [connection_name, generate_sub_model(connection_name)]
      end
      @sub_models = Hash[sub_models]
    end

    # @param [Symbol] connection_name
    # @return [Class] A sub model of given base class
    def fetch(connection_name)
      @sub_models.fetch(connection_name)
    end

    # @return [Array<Class>]
    def all
      @sub_models.values
    end

    private

    # @param [Symbol] connection_name
    # @return [Class] A generated sub class of given AR model
    def generate_sub_model(connection_name)
      base_class_name = @base_class.name
      class_name = generate_class_name(connection_name)

      sub_model = Class.new(base_class) do
        self.table_name = base_class.table_name
        module_eval <<-RUBY, __FILE__, __LINE__ + 1
          def self.name
            "#{base_class_name}::#{class_name}"
          end
        RUBY
      end
      sub_model.class_eval { establish_connection(connection_name) }
      sub_model
    end

    # @param [Symbol] name
    # @return [String]
    def generate_class_name(name)
      "GeneratedModel#{name.to_s.gsub('-', '_').classify}"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mixed_gauge-0.2.1 lib/mixed_gauge/sub_model_repository.rb
mixed_gauge-0.2.0 lib/mixed_gauge/sub_model_repository.rb
mixed_gauge-0.1.4 lib/mixed_gauge/sub_model_repository.rb
mixed_gauge-0.1.3 lib/mixed_gauge/sub_model_repository.rb
mixed_gauge-0.1.2 lib/mixed_gauge/sub_model_repository.rb
mixed_gauge-0.1.1 lib/mixed_gauge/sub_model_repository.rb
mixed_gauge-0.1.0 lib/mixed_gauge/sub_model_repository.rb