trax_model 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trax/model.rb +1 -0
- data/lib/trax/model/enum.rb +52 -0
- data/lib/trax_model/version.rb +1 -1
- data/trax_model.gemspec +2 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea648e5e27dcfea15c70cc7a42b75089f6a3511f
|
4
|
+
data.tar.gz: 611952b4b1f4031bf17d5e1857addabd371bb823
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 267094cef9ff8161ebc7029f41d59998495578fc17658aa41c5abb343be1fe31cae7e85d75e2ed4f37d196d80919f17d237b0500a2d88933d127759ebb7f479a
|
7
|
+
data.tar.gz: 456a2dbc24cddfabe0975be863ba62e4d4008630d0e1d551faa4378f2a5efc7abb095e98ba60df6410cf3f2a45883a387bcdf9b74e582e9046ec6cf0728becc9
|
data/lib/trax/model.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
# An opinionated enum preconfiguration:
|
2
|
+
# Uses simple_enum instead of active record built in enum, due to the complete
|
3
|
+
# and utter polution it causes to your classes. in addition, AR does not prefix
|
4
|
+
# enums with anything, i.e.
|
5
|
+
# class User
|
6
|
+
# enum :gender => [:unknown, :male, :female]
|
7
|
+
# enum :locale => [:unknown, :en, :ca]
|
8
|
+
# will break because :unknown cant be used twice, since it defines an User.unknown
|
9
|
+
# method, the value which does not even make sense in alot of situations, out of context
|
10
|
+
#
|
11
|
+
# So, we use simple_enum instead, which will allow us to do that
|
12
|
+
# in addition, I dont like that simple_enum does not allow you to raise errrors at the activerecord
|
13
|
+
# level using validations, therefore you cant reuse the same interface for displaying errors
|
14
|
+
# to the user if an invalid value is picked
|
15
|
+
# so validation is added when an enum is defined
|
16
|
+
# also defines just 2 generic scopes, by_enum_name, and by_enum_name_not, which accept multiple args
|
17
|
+
# i.e. by_gender(:male), by_gender_not(:female, :unknown)
|
18
|
+
# in interest of not polluting the class with scopes for each individual enum value
|
19
|
+
|
20
|
+
module Trax
|
21
|
+
module Model
|
22
|
+
module Enum
|
23
|
+
extend ::ActiveSupport::Concern
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
def as_enum(enum_name, args, options = {})
|
27
|
+
options.assert_valid_keys(:prefix, :source, :message)
|
28
|
+
options[:message] ||= "Is not a valid value for #{enum_name}"
|
29
|
+
options[:prefix] ||= true
|
30
|
+
options[:source] ||= enum_name
|
31
|
+
enum_values = args.is_a?(Hash) ? args.keys : args
|
32
|
+
validation_options = { :in => enum_values, :message => options.extract!(:message) }
|
33
|
+
|
34
|
+
self.validates_inclusion_of(enum_name, validation_options)
|
35
|
+
scope_method_name = :"by_#{enum_name}"
|
36
|
+
scope_not_method_name = :"by_#{enum_name}_not"
|
37
|
+
|
38
|
+
self.scope scope_method_name, lambda { |*values|
|
39
|
+
enum_hash = self.__send__("#{enum_name}".pluralize).hash
|
40
|
+
where(enum_name => enum_hash.with_indifferent_access.slice(*values.flatten.compact.uniq).values)
|
41
|
+
}
|
42
|
+
self.scope scope_not_method_name, lambda { |*values|
|
43
|
+
enum_hash = self.__send__("#{enum_name}".pluralize).hash
|
44
|
+
where.not(enum_name => enum_hash.with_indifferent_access.slice(*values.flatten.compact.uniq).values)
|
45
|
+
}
|
46
|
+
|
47
|
+
super(enum_name, args, options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/trax_model/version.rb
CHANGED
data/trax_model.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_dependency "trax_core", "~> 0.0.3"
|
22
22
|
spec.add_dependency "default_value_for", "~> 3.0.0"
|
23
|
+
spec.add_dependency "simple_enum"
|
23
24
|
spec.add_development_dependency "activerecord"
|
24
25
|
spec.add_development_dependency "bundler", "~> 1.6"
|
25
26
|
spec.add_development_dependency "rake"
|
@@ -35,4 +36,5 @@ Gem::Specification.new do |spec|
|
|
35
36
|
spec.add_development_dependency 'guard-bundler', '~> 2'
|
36
37
|
spec.add_development_dependency 'rb-fsevent'
|
37
38
|
spec.add_development_dependency 'terminal-notifier-guard'
|
39
|
+
|
38
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trax_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Ayre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: trax_core
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simple_enum
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: activerecord
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -266,6 +280,7 @@ files:
|
|
266
280
|
- lib/trax.rb
|
267
281
|
- lib/trax/model.rb
|
268
282
|
- lib/trax/model/config.rb
|
283
|
+
- lib/trax/model/enum.rb
|
269
284
|
- lib/trax/model/errors.rb
|
270
285
|
- lib/trax/model/freezable.rb
|
271
286
|
- lib/trax/model/matchable.rb
|
@@ -323,7 +338,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
323
338
|
version: '0'
|
324
339
|
requirements: []
|
325
340
|
rubyforge_project:
|
326
|
-
rubygems_version: 2.
|
341
|
+
rubygems_version: 2.2.2
|
327
342
|
signing_key:
|
328
343
|
specification_version: 4
|
329
344
|
summary: Higher level ActiveRecord models for rails
|