Sha256: f5b0ad07fcd03dedb3ede53374ac10eb1b15c2686cadb52872c3b97e85b4d573

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module ToFactory
  class FileSync
    def initialize(m  = ToFactory::Finders::Model.new,
                   fw = ToFactory::FileWriter.new,
                   ff = ToFactory::Finders::Factory.new)
      @model_finder = wrap_model(m)
      @file_writer  = fw
      @factory_finder = ff
    end

    def perform(exclusions=[])
      @file_writer.write(all_representations exclusions)
    end

    def all_representations(exclusions=[])
      Collation.organize(
        new_representations(exclusions),
        existing_representations)
    end

    def new_representations(exclusions=[])
      instances = @model_finder.call(exclusions)

      instances.map{|r| Representation.from(r) }
    end

    private

    def existing_representations
      @factory_finder.call
    end

    def wrap_model(m)
      if m.respond_to?(:call)
        m
      else
        lambda{|exclusions|
          exclusions ||= []
          records = if m.is_a?(ActiveRecord::Base)
            Array m
          else
            m
          end

          records.reject{|o,_| exclusions.include?(o.class)}
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
to_factory-2.0.0 lib/to_factory/file_sync.rb
to_factory-0.2.1 lib/to_factory/file_sync.rb
to_factory-0.2.0 lib/to_factory/file_sync.rb