Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development for v3 #101

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
language: ruby
services: mongodb
sudo: false
rvm:
- 1.9.3
- 2.0.0
- 2.1.1
- 2.2.0
- ruby-head
- jruby-19mode

gemfile:
- Gemfile
- gemfiles/4.0.gemfile
- gemfiles/5.0.gemfile

#matrix:
# allow_failures:
# - rvm: jruby-19mode
matrix:
allow_failures:
- rvm: 2.0.0
- rvm: jruby-19mode

env:
global:
Expand Down
15 changes: 15 additions & 0 deletions gemfiles/5.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
source "http://rubygems.org"

gem 'activesupport', '5.0.0.alpha', github: 'rails/rails', branch: 'master'
gem 'activerecord', '5.0.0.alpha', github: 'rails/rails', branch: 'master'
gem 'arel', '7.0.0.alpha', github: 'rails/arel', branch: 'master'

# some development deps
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
gem 'sqlite3', platform: :ruby
gem 'bson_ext', platform: :ruby

# Code coverage on CI only
gem 'codeclimate-test-reporter', group: :test, require: nil

gemspec path: "../"
2 changes: 1 addition & 1 deletion lib/simple_enum/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def generate_enum_scope_methods_for(enum, accessor)
return unless respond_to?(:scope)

enum.each_pair do |key, value|
scope "#{accessor.prefix}#{key.pluralize}", -> { accessor.scope(self, value) }
scope "#{accessor.prefix}#{key.to_s.pluralize}", -> { accessor.scope(self, value) }
end
end
end
Expand Down
14 changes: 10 additions & 4 deletions lib/simple_enum/enum.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require 'active_support/core_ext/string'
require 'active_support/core_ext/hash'

module SimpleEnum
class Enum
attr_reader :name, :hash
attr_reader :name

def initialize(name, hash)
@name = name.to_s
@hash = hash
@symbols_hash = hash.symbolize_keys
end

def include?(key)
Expand All @@ -26,16 +28,16 @@ def value(key)
alias_method :[], :value

def each_pair(&block)
hash.each_pair(&block)
symbols_hash.each_pair(&block)
end
alias_method :each, :each_pair

def map(&block)
hash.map(&block)
symbols_hash.map(&block)
end

def keys
hash.keys
symbols_hash.keys
end

def values_at(*keys)
Expand All @@ -46,5 +48,9 @@ def values_at(*keys)
def to_s
name
end

# Private access to hash and symbolized hash
private
attr_reader :hash, :symbols_hash
end
end
2 changes: 1 addition & 1 deletion lib/simple_enum/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module SimpleEnum

# The current `SimpleEnum` version.
VERSION = "2.1.1"
VERSION = "3.0.0.dev"
end
1 change: 1 addition & 0 deletions lib/simple_enum/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def enum_option_pairs(record, enum, encode_as_value = false)
record = record.class unless record.respond_to?(reader)

record.send(reader).map { |key, value|
key = key.to_s
name = record.human_enum_name(enum, key) if record.respond_to?(:human_enum_name)
name ||= translate_enum_key(enum, key)
[name, encode_as_value ? value : key]
Expand Down
2 changes: 1 addition & 1 deletion simple_enum.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gem::Specification.new do |s|
s.summary = "Simple enum-like field support for models."
s.description = "Provides enum-like fields for ActiveRecord, ActiveModel and Mongoid models."

s.required_ruby_version = ">= 1.9.3"
s.required_ruby_version = ">= 2.0.0"
s.required_rubygems_version = ">= 2.0.0"

s.authors = ["Lukas Westermann"]
Expand Down
16 changes: 8 additions & 8 deletions spec/simple_enum/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,22 @@ def generate_enum_spec_extension_for(enum, accessor)
end

it 'delegates #male? to accessor' do
expect(accessor).to receive(:selected?).with(subject, 'male') { true }
expect(accessor).to receive(:selected?).with(subject, :male) { true }
expect(subject.male?).to be_truthy
end

it 'delegates #female? to accessor' do
expect(accessor).to receive(:selected?).with(subject, 'female') { false }
expect(accessor).to receive(:selected?).with(subject, :female) { false }
expect(subject.female?).to be_falsey
end

it 'delegates #male! to accessor' do
expect(accessor).to receive(:write).with(subject, 'male') { 0 }
expect(accessor).to receive(:write).with(subject, :male) { 0 }
expect(subject.male!).to eq 0
end

it 'delegates #female! to accessor' do
expect(accessor).to receive(:write).with(subject, 'female') { 1 }
expect(accessor).to receive(:write).with(subject, :female) { 1 }
expect(subject.female!).to eq 1
end
end
Expand All @@ -164,22 +164,22 @@ def generate_enum_spec_extension_for(enum, accessor)
end

it 'delegates #gender_male? to accessor' do
expect(accessor).to receive(:selected?).with(subject, 'male') { true }
expect(accessor).to receive(:selected?).with(subject, :male) { true }
expect(subject.gender_male?).to be_truthy
end

it 'delegates #gender_female? to accessor' do
expect(accessor).to receive(:selected?).with(subject, 'female') { false }
expect(accessor).to receive(:selected?).with(subject, :female) { false }
expect(subject.gender_female?).to be_falsey
end

it 'delegates #gender_male! to accessor' do
expect(accessor).to receive(:write).with(subject, 'male') { 0 }
expect(accessor).to receive(:write).with(subject, :male) { 0 }
expect(subject.gender_male!).to eq 0
end

it 'delegates #gender_female! to accessor' do
expect(accessor).to receive(:write).with(subject, 'female') { 1 }
expect(accessor).to receive(:write).with(subject, :female) { 1 }
expect(subject.gender_female!).to eq 1
end
end
Expand Down
20 changes: 7 additions & 13 deletions spec/simple_enum/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,23 @@
end
end

context '#hash' do
subject { described_class.new(:gender, hash).hash }

it 'returns hash that was set in the constructor' do
expect(subject).to be_a(Hash)
expect(subject.keys).to eq %w{male female}
expect(subject.values).to eq [0, 1]
end
end

context '#keys' do
it 'returns the keys in the order added' do
expect(subject.keys).to eq %w{male female}
expect(subject.keys).to eq [:male, :female]
end
end

context '#each_pair (aliased to #each)' do
it 'yields twice with #each_pair' do
expect { |b| subject.each_pair(&b) }.to yield_control.exactly(2).times
result = []
subject.each_pair { |b| result << b }
expect(result).to eq [[:male, 0], [:female, 1]]
end

it 'yields twice with #each' do
expect { |b| subject.each(&b) }.to yield_control.exactly(2).times
result = []
subject.each { |b| result << b }
expect(result).to eq [[:male, 0], [:female, 1]]
end
end

Expand Down