Skip to content

make sure getters always return Symbols #30

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

Open
wants to merge 8 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
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
script: "bundle exec rspec spec"

sudo: false
language: ruby

rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.1.1
- 2.1.2
- jruby-19mode

services:
- mongodb
15 changes: 7 additions & 8 deletions lib/mongoid/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def enum(name, values, options = {})

create_field field_name, options

create_validations field_name, values, options
create_validations name, field_name, options
define_value_scopes_and_accessors field_name, values, options
define_field_accessor name, field_name, options
end
Expand All @@ -40,12 +40,11 @@ def create_field(field_name, options)
field field_name, :type => type, :default => options[:default]
end

def create_validations(field_name, values, options)
def create_validations(name, field_name, options)
if options[:multiple] && options[:validate]
validates field_name, :'mongoid/enum/validators/multiple' => { :in => values.map(&:to_sym), :allow_nil => !options[:required] }
#FIXME: Shouldn't this be `elsif options[:validate]` ???
elsif validate
validates field_name, :inclusion => {:in => values.map(&:to_sym)}, :allow_nil => !options[:required]
validates field_name, :'mongoid/enum/validators/multiple' => { :in => self.const_get(name.to_s.upcase), :allow_nil => !options[:required] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [149/80]
Use the new Ruby 1.9 hash syntax.
Redundant self detected.

elsif options[:validate]
validates field_name, :inclusion => {:in => self.const_get(name.to_s.upcase)}, :allow_nil => !options[:required]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [122/80]
Use the new Ruby 1.9 hash syntax.
Redundant self detected.
Space inside { missing.
Space inside } missing.

end
end

Expand All @@ -71,12 +70,12 @@ def define_field_accessor(name, field_name, options)

def define_array_field_accessor(name, field_name)
class_eval "def #{name}=(vals) self.write_attribute(:#{field_name}, Array(vals).compact.map(&:to_sym)) end"
class_eval "def #{name}() self.read_attribute(:#{field_name}) end"
class_eval "def #{name}() self.send(:#{field_name}).map{ |i| i.try(:to_sym) } end"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [90/80]

end

def define_string_field_accessor(name, field_name)
class_eval "def #{name}=(val) self.write_attribute(:#{field_name}, val && val.to_sym || nil) end"
class_eval "def #{name}() self.read_attribute(:#{field_name}) end"
class_eval "def #{name}() self.send(:#{field_name}) end"
end

def define_array_accessor(field_name, value)
Expand Down
2 changes: 1 addition & 1 deletion mongoid-enum.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "mongoid", "~> 5.0"
spec.add_runtime_dependency "mongoid"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
Expand Down