numeritaj_tipo 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -4
- data/lib/generators/enums/templates/enum.rb +3 -10
- data/lib/numeritaj_tipo.rb +10 -0
- data/lib/numeritaj_tipo/active_record/type/enum_type.rb +1 -1
- data/lib/numeritaj_tipo/attribute_definition.rb +4 -2
- data/lib/numeritaj_tipo/enum_base.rb +2 -28
- data/lib/numeritaj_tipo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10cd8b3f8353e68b40cf8245b12649141328d888
|
4
|
+
data.tar.gz: 981c529a03cae98a97e7271f8d24135e8527f61b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20fa4e3898c77b0691ded256ad3ea6384cce26b8a8aec44eee5711772fb425d8e26432d9e96180db5616e2db9e7ce0cc4aedbf3cf81acdbe24b873f52c7c6745
|
7
|
+
data.tar.gz: cc2ff797d5bb5fd7dca824c0ecc851a8d253288be32609e28bef78e0ec27b3ddca4e5f31fd12228e347a56ca221906dc7f2c539393314d524e8892235fd1675b
|
data/README.md
CHANGED
@@ -25,7 +25,7 @@ Or install it yourself as:
|
|
25
25
|
### ActiveRecord
|
26
26
|
```ruby
|
27
27
|
class User < ActiveRecord::Base
|
28
|
-
include NumeritajTipo
|
28
|
+
include NumeritajTipo::ActiveRecord
|
29
29
|
|
30
30
|
enumerize :role, values: %i(admin user), type: Symbol, default: :user
|
31
31
|
end
|
@@ -35,7 +35,7 @@ end
|
|
35
35
|
```ruby
|
36
36
|
class User
|
37
37
|
include Mongoid::Document
|
38
|
-
include NumeritajTipo
|
38
|
+
include NumeritajTipo::Mongoid
|
39
39
|
|
40
40
|
field :name
|
41
41
|
enumerize :role, values: %i(admin user), type: Symbol, default: :user
|
@@ -46,13 +46,24 @@ end
|
|
46
46
|
```ruby
|
47
47
|
class User
|
48
48
|
include Neo4j::ActiveNode
|
49
|
-
include NumeritajTipo
|
49
|
+
include NumeritajTipo::Neo4j
|
50
50
|
|
51
51
|
property :name
|
52
52
|
enumerize :role, values: %i(admin user), type: Symbol, default: :user
|
53
53
|
end
|
54
54
|
```
|
55
55
|
|
56
|
+
#### enumerize options
|
57
|
+
* values:
|
58
|
+
* enum values range.
|
59
|
+
* values should be Symbol values list.
|
60
|
+
* type:
|
61
|
+
* enum type (describe below).
|
62
|
+
* default:
|
63
|
+
* default value of the attribute.
|
64
|
+
* allow_nil
|
65
|
+
* if `true` allow the null value, it doesn't allow in the case of `false`.
|
66
|
+
|
56
67
|
#### predicate methods
|
57
68
|
```ruby
|
58
69
|
user = User.create
|
@@ -86,11 +97,21 @@ User.user.count
|
|
86
97
|
$ rails g enum Role admin user
|
87
98
|
```
|
88
99
|
|
89
|
-
will generate enum
|
100
|
+
will generate enum type under `app/enums`:
|
101
|
+
|
102
|
+
* app/enums/role.rb:
|
103
|
+
```ruby
|
104
|
+
NumritajTipo.define_enum :Role, %i{
|
105
|
+
admin
|
106
|
+
user
|
107
|
+
}
|
108
|
+
```
|
90
109
|
|
91
110
|
* using enum
|
92
111
|
```ruby
|
93
112
|
class User < ActiveRecord::Base
|
113
|
+
include NumeritajTipo::ActiveRecord
|
114
|
+
|
94
115
|
enumerize :role, type: 'Role', default: :user
|
95
116
|
end
|
96
117
|
```
|
@@ -1,10 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
<% values.each do |arg| %>
|
5
|
-
class <%= "#{class_name}::#{arg.camelize}" %> < <%= class_name %>
|
6
|
-
def initialize
|
7
|
-
@value = :<%= arg %>
|
8
|
-
end
|
9
|
-
end
|
10
|
-
<%- end -%>
|
1
|
+
NumeritajTipo.define_enum :<%= class_name %>, %i{
|
2
|
+
<% values %>
|
3
|
+
}
|
data/lib/numeritaj_tipo.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_support'
|
2
|
+
require 'active_support/inflector'
|
2
3
|
require 'numeritaj_tipo/version'
|
3
4
|
require 'numeritaj_tipo/railtie' if defined?(Rails)
|
4
5
|
|
@@ -13,4 +14,13 @@ module NumeritajTipo
|
|
13
14
|
autoload :ActiveRecord
|
14
15
|
autoload :Mongoid
|
15
16
|
autoload :Neo4j
|
17
|
+
|
18
|
+
def define_enum(enum, values)
|
19
|
+
Object.const_set enum, Class.new(EnumBase)
|
20
|
+
|
21
|
+
enum_type = enum.to_s.constantize
|
22
|
+
enum_type.cattr_accessor :values
|
23
|
+
enum_type.values = values
|
24
|
+
end
|
25
|
+
module_function :define_enum
|
16
26
|
end
|
@@ -4,10 +4,11 @@ module NumeritajTipo
|
|
4
4
|
|
5
5
|
delegate :type, :enum_values, :values_for_validation, to: :@enum_type
|
6
6
|
|
7
|
-
def initialize(name, values: nil, type: Symbol, default: nil)
|
7
|
+
def initialize(name, values: nil, type: Symbol, default: nil, allow_nil: false)
|
8
8
|
@name = name
|
9
9
|
@enum_type = EnumType.new(type, values)
|
10
10
|
@default = default
|
11
|
+
@allow_nil = allow_nil
|
11
12
|
end
|
12
13
|
|
13
14
|
def build(target)
|
@@ -57,7 +58,8 @@ module NumeritajTipo
|
|
57
58
|
end
|
58
59
|
|
59
60
|
def define_validation!(target)
|
60
|
-
target.validates_inclusion_of name, in: values_for_validation
|
61
|
+
target.validates_inclusion_of name, in: values_for_validation,
|
62
|
+
allow_nil: @allow_nil
|
61
63
|
end
|
62
64
|
end
|
63
65
|
end
|
@@ -1,35 +1,9 @@
|
|
1
|
-
require 'active_support'
|
2
|
-
require 'active_support/core_ext/class/subclasses'
|
3
|
-
|
4
1
|
module NumeritajTipo
|
5
2
|
class EnumBase
|
6
|
-
include Comparable
|
7
|
-
|
8
|
-
attr_reader :value
|
9
|
-
|
10
3
|
class << self
|
11
|
-
def build(
|
12
|
-
|
13
|
-
|
14
|
-
subclasses.find {|subclass| subclass.new.value == value }.new
|
4
|
+
def build(other)
|
5
|
+
values.find {|value| value == other.to_sym }
|
15
6
|
end
|
16
|
-
|
17
|
-
def values
|
18
|
-
subclasses.map {|subclass| subclass.new.value }
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def ==(other)
|
23
|
-
self.value == other
|
24
|
-
end
|
25
|
-
|
26
|
-
def to_s
|
27
|
-
value.to_s
|
28
|
-
end
|
29
|
-
|
30
|
-
def to_sym
|
31
|
-
value
|
32
7
|
end
|
33
|
-
alias :intern :to_sym
|
34
8
|
end
|
35
9
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: numeritaj_tipo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kajisha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|