Sha256: 746760be958add86ac4f135c8e7fe81110278753f19078a36344e58ae02729c9

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module Columns

  # Add schema info to the end of a ruby model.
  class ModelWriter

    # Public: Creates a new ModelWriter.
    #
    # path - String relative or absolute path to the models directory.
    #        It's handy to be able to change the path during tests.
    #        Default is './app/models/'.
    def initialize(path: './app/models/')
      @path = path
    end

    # Public: Puts model data and meta data to the end of a model file.
    #
    # model_data - A ModelData, used to deduce the model file and to
    #              grab data.
    #
    # Returns nothing.
    def add_info(model_data)
      @model_data = model_data
      ensure_file_end_with_empty_line
      if File.exists?(model_path)
        File.open(model_path, 'a') do |file|
          file.puts(ExtendedContent.from(@model_data.content))
        end
      end
    end

    private

    def model_path
      File.expand_path(File.join(@path, @model_data.name) + '.rb')
    end

    def ensure_file_end_with_empty_line
      %x( sed -i -e '$a\\' #{model_path} > /dev/null 2>&1 )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
columns-0.2.0 lib/columns/model_writer.rb
columns-0.1.1 lib/columns/model_writer.rb
columns-0.1.0 lib/columns/model_writer.rb