diff --git a/.travis.yml b/.travis.yml index b077d90..ea5292f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/gemfiles/5.0.gemfile b/gemfiles/5.0.gemfile new file mode 100644 index 0000000..4e7f597 --- /dev/null +++ b/gemfiles/5.0.gemfile @@ -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: "../" diff --git a/lib/simple_enum/attribute.rb b/lib/simple_enum/attribute.rb index 0da142a..079c3eb 100644 --- a/lib/simple_enum/attribute.rb +++ b/lib/simple_enum/attribute.rb @@ -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 diff --git a/lib/simple_enum/enum.rb b/lib/simple_enum/enum.rb index 9e8f431..da19b36 100644 --- a/lib/simple_enum/enum.rb +++ b/lib/simple_enum/enum.rb @@ -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) @@ -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) @@ -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 diff --git a/lib/simple_enum/version.rb b/lib/simple_enum/version.rb index 3ea2434..00b8a8c 100644 --- a/lib/simple_enum/version.rb +++ b/lib/simple_enum/version.rb @@ -1,5 +1,5 @@ module SimpleEnum # The current `SimpleEnum` version. - VERSION = "2.1.1" + VERSION = "3.0.0.dev" end diff --git a/lib/simple_enum/view_helpers.rb b/lib/simple_enum/view_helpers.rb index 136bd72..1836b81 100644 --- a/lib/simple_enum/view_helpers.rb +++ b/lib/simple_enum/view_helpers.rb @@ -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] diff --git a/simple_enum.gemspec b/simple_enum.gemspec index 0430aa2..ea1011e 100644 --- a/simple_enum.gemspec +++ b/simple_enum.gemspec @@ -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"] diff --git a/spec/simple_enum/attribute_spec.rb b/spec/simple_enum/attribute_spec.rb index 2483bf1..107293f 100644 --- a/spec/simple_enum/attribute_spec.rb +++ b/spec/simple_enum/attribute_spec.rb @@ -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 @@ -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 diff --git a/spec/simple_enum/enum_spec.rb b/spec/simple_enum/enum_spec.rb index c8e3070..5c16c8a 100644 --- a/spec/simple_enum/enum_spec.rb +++ b/spec/simple_enum/enum_spec.rb @@ -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