Sha256: 05962c65093d3b1ca45f9f1908422db8d0b135651b28d48f94a9e8f631905df3

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

describe SimpleFlaggableColumn do
  with_model :GameModel do
    table do |t|
      t.integer :platforms, default: 0, null: false
    end

    model do
      include SimpleFlaggableColumn

      flag_column :platforms, {
        win:   0b001,
        mac:   0b010,
        linux: 0b100
      }
    end
  end

  it 'should be empty by default' do
    expect(GameModel.new.platforms).to eq []
  end

  it 'should let you save data' do
    game = GameModel.new
    game.platforms = [:mac, :linux]
    expect(game.read_attribute(:platforms)).to eq 0b110
    expect(game.platforms).to match_array [:mac, :linux]
  end

  it "should ignore items that aren't on the list" do
    game = GameModel.new
    game.platforms = [:mac, :linux, :potato]
    expect(game.read_attribute(:platforms)).to eq 0b110
    expect(game.platforms).to match_array [:mac, :linux]
  end

  it 'should set the value to 0 when nil' do
    game = GameModel.new
    game.platforms = [:mac, :linux]
    expect(game.platforms).to eq [:mac, :linux]
    game.platforms = nil
    expect(game.platforms).to eq []
  end

  it 'should allow you to set numeric values directly' do
    game = GameModel.new
    game.platforms = 0b101
    expect(game.platforms).to eq [:win, :linux]
  end

  describe 'flags lists' do
    it 'should define the list of flags' do
      expect(GameModel.platforms_flags).to eq({
        win:   0b001,
        mac:   0b010,
        linux: 0b100
      })
    end

    it 'should let you get a join list of flags' do
      expect(GameModel.platforms_flags(:win, :linux)).to eq 0b101
    end

    it 'should define the reverse list of flags' do
      expect(GameModel.flags_platforms).to eq({
        0b001 => :win,
        0b010 => :mac,
        0b100 => :linux
      })
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_flaggable_column-0.0.2 spec/simple_flaggable_column/simple_flaggable_column_spec.rb