Sha256: fda486b279666e5e2c15be28aa5a815734bc730ff74e952c0119ca3a019a0151

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

module ModelFactories

  def create_source(custom={})
    create_model!(DataCatalog::Source, custom, {
      :title => "Healthcare Spending Data",
      :url   => "http://data.gov/details/23",
    })
  end

  def create_category(custom={})
    create_model!(DataCatalog::Category, custom, {
      :name => "Sample Category",
    })
  end

  def create_categorization(custom={})
    create_model!(DataCatalog::Categorization, custom, {
      :source_id   => "",
      :category_id => "",
    })
  end

  def create_note(custom={})
    create_model!(DataCatalog::Note, custom, {
      :text    => "Sample Note",
      :user_id => "",
    })
  end

  def create_user(custom={})
    create_model!(DataCatalog::User, custom, {
      :name  => "Sample User",
      :email => "sample.user@inter.net",
      :role  => "basic",
    })
  end

  # -----

  def new_usage(custom={})
    new_model!(DataCatalog::Usage, custom, {
      :title       => "Sample Usage",
      :url         => "http://inter.net/article/100",
      :description => "Citation from the internet",
    })
  end

  protected

  def create_model!(klass, custom, required)
    model = klass.create(required.merge(custom))
    unless model.valid?
      raise "Invalid #{klass}: #{model.errors.errors.inspect}"
    end
    model
  end

  def new_model!(klass, custom, required)
    model = klass.new(required.merge(custom))
    unless model.valid?
      raise "Invalid #{klass}: #{model.errors.errors.inspect}"
    end
    model
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sinatra_resource-0.4.24 examples/datacatalog/test/helpers/lib/model_factories.rb
sinatra_resource-0.4.23 examples/datacatalog/test/helpers/lib/model_factories.rb
sinatra_resource-0.4.22 examples/datacatalog/test/helpers/lib/model_factories.rb