active_enum 1.0.0.rc1 → 1.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: abc265242b6f277fd73cd50f675eb3e966407cda
4
- data.tar.gz: 5b4c18dd92b26c6282afa3ba13e131d9ac916bd0
2
+ SHA256:
3
+ metadata.gz: e0463e9528b9a2ec019caf593cfc2becaa1767a116d4657d93de5462c8fb5475
4
+ data.tar.gz: 2cd9be66544fa2bd00cf2ace406c4c5010411f02d53f1c926d0f197609e89993
5
5
  SHA512:
6
- metadata.gz: c0a0d1e62906848e9a031e6bb8cd27071a3fa4b2b6919d4311eb7fbae2fcfb1676f9a6d292d94d462a167d2b084cccc795e37d58d65c0af25f6db5a9bf1f08f8
7
- data.tar.gz: 5c9cb81a882a6ddaf6c2e3c6d864598e85d544e91f835ea711b9881a2c710b1b20b5812987481dc50975ee13ff60814cda8967fc36058f67dfed5a873b45947e
6
+ metadata.gz: efd7a02386a9e3a6a45812b35e8c509ac60d47249238bee485b00fc8d0a1598ed8a0ae5319e02d76f59ca06ccce5e22dfd25ad0b217ee29f7471510a8deeb15c
7
+ data.tar.gz: d69b2b354382981ba79af48b3d0306839a1f218d7e7bf01136a2021fdc7026a4b0eedfc40e00e5d63c24deaf2eccf00b5727083a8a2023947a29a00ed579a5ec
@@ -17,37 +17,49 @@ module ActiveEnum
17
17
  module ClassMethods
18
18
 
19
19
  def ids
20
- enum_values.map {|v| v.id }
20
+ enum_values.map { |v| v.id }
21
21
  end
22
22
 
23
23
  def names
24
- enum_values.map {|v| v.send(active_enum_options[:name_column]) }
24
+ enum_values.map { |v| v.send(active_enum_options[:name_column]) }
25
25
  end
26
26
 
27
27
  def to_select
28
- enum_values.map {|v| [v.send(active_enum_options[:name_column]), v.id] }
28
+ enum_values.map { |v| [v.send(active_enum_options[:name_column]), v.id] }
29
29
  end
30
30
 
31
31
  def [](index)
32
- if index.is_a?(Integer)
33
- v = lookup_by_id(index)
34
- v.send(active_enum_options[:name_column]) unless v.blank?
35
- else
36
- v = lookup_by_name(index)
37
- v.id unless v.blank?
38
- end
32
+ row = get_value(index)
33
+ return if row.nil?
34
+ index.is_a?(Integer) ? row.send(active_enum_options[:name_column]) : row.id
39
35
  end
40
36
 
41
- private
42
-
43
- def lookup_by_id(index)
44
- enum_values.find_by_id(index)
37
+ # Access any meta data defined for a given id or name. Returns a hash.
38
+ def meta(index)
39
+ row = lookup_relation(index).unscope(:select).first
40
+ row&.attributes.except(primary_key.to_s, active_enum_options[:name_column].to_s)
45
41
  end
46
42
 
47
- def lookup_by_name(index)
48
- enum_values.where("#{active_enum_options[:name_column]} like lower(?)", index.to_s).first
43
+ # Enables use as a delimiter in inclusion validation
44
+ def include?(value)
45
+ !self[value].nil?
49
46
  end
50
47
 
48
+ private
49
+
50
+ def get_value(index)
51
+ lookup_relation(index).first || begin
52
+ raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if ActiveEnum.raise_on_not_found
53
+ end
54
+ end
55
+
56
+ def lookup_relation(index)
57
+ if index.is_a?(Integer)
58
+ enum_values.where(id: index)
59
+ else
60
+ enum_values.where("#{active_enum_options[:name_column]} like lower(?)", index.to_s)
61
+ end
62
+ end
51
63
  end
52
64
 
53
65
  end
@@ -20,7 +20,7 @@ module ActiveEnum
20
20
  # value 1 => 'Foo'
21
21
  #
22
22
  def value(enum_value)
23
- store.set *id_and_name_and_meta(enum_value)
23
+ store.set(*id_and_name_and_meta(enum_value))
24
24
  end
25
25
 
26
26
  # Specify order enum values are returned.
@@ -44,17 +44,17 @@ module ActiveEnum
44
44
 
45
45
  # Array of all enum id values
46
46
  def ids
47
- store.values.map {|v| v[0] }
47
+ store.values.map { |v| v[0] }
48
48
  end
49
49
 
50
50
  # Array of all enum name values
51
51
  def names
52
- store.values.map {|v| v[1] }
52
+ store.values.map { |v| v[1] }
53
53
  end
54
54
 
55
55
  # Return enum values in an array suitable to pass to a Rails form select helper.
56
56
  def to_select
57
- store.values.map {|v| [v[1], v[0]] }
57
+ store.values.map { |v| [v[1], v[0]] }
58
58
  end
59
59
 
60
60
  # Access id or name value. Pass an id number to retrieve the name or
@@ -66,15 +66,6 @@ module ActiveEnum
66
66
  end
67
67
  alias_method :[], :get
68
68
 
69
- # Access value row array for a given id or name value.
70
- def get_value(index)
71
- if index.is_a?(Integer)
72
- store.get_by_id(index)
73
- else
74
- store.get_by_name(index)
75
- end || (ActiveEnum.raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
76
- end
77
-
78
69
  def include?(value)
79
70
  !get(value).nil?
80
71
  end
@@ -87,6 +78,15 @@ module ActiveEnum
87
78
 
88
79
  private
89
80
 
81
+ # Access value row array for a given id or name value.
82
+ def get_value(index)
83
+ if index.is_a?(Integer)
84
+ store.get_by_id(index)
85
+ else
86
+ store.get_by_name(index)
87
+ end || (ActiveEnum.raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
88
+ end
89
+
90
90
  def id_and_name_and_meta(hash)
91
91
  if hash.has_key?(:name)
92
92
  id = hash.delete(:id) || next_id
@@ -64,7 +64,7 @@ module ActiveEnum
64
64
  enum_class_name = "#{name}::#{attribute.to_s.camelize}"
65
65
  eval("class #{enum_class_name} < ActiveEnum::Base; end")
66
66
  enum = enum_class_name.constantize
67
- enum.class_eval &block
67
+ enum.class_eval(&block)
68
68
  enum
69
69
  end
70
70
 
@@ -14,7 +14,7 @@ module ActiveEnum
14
14
  end
15
15
 
16
16
  def values
17
- _values.map { |(id, name)| get_by_id(id) }
17
+ _values.map { |(id, _)| get_by_id(id) }
18
18
  end
19
19
 
20
20
  def i18n_scope
@@ -4,7 +4,7 @@ module ActiveEnum
4
4
 
5
5
  def set(id, name, meta=nil)
6
6
  check_duplicate id, name
7
- _values << [id, name.to_s, meta].compact
7
+ _values << [ id, name.to_s, meta ].compact
8
8
  sort!
9
9
  end
10
10
 
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '1.0.0.rc1'
2
+ VERSION = '1.0.0.rc2'
3
3
  end
@@ -30,24 +30,34 @@ describe ActiveEnum::ActsAsEnum do
30
30
  expect(TestPerson.extended_modules).to include(ActiveEnum::ActsAsEnum::ClassMethods)
31
31
  end
32
32
 
33
- it "should return name column value when passing id to [] method" do
34
- expect(Person[1]).to eq('Dave')
35
- end
33
+ context "#[]" do
34
+ it "should return name column value when passing id to [] method" do
35
+ expect(Person[1]).to eq('Dave')
36
+ end
36
37
 
37
- it "should return id column value when passing string name to [] method" do
38
- expect(Person['Dave']).to eq(1)
39
- expect(Person['dave']).to eq(1)
40
- end
38
+ it "should return id column value when passing string name to [] method" do
39
+ expect(Person['Dave']).to eq(1)
40
+ expect(Person['dave']).to eq(1)
41
+ end
41
42
 
42
- it "should return id column value when passing symbol name to [] method" do
43
- expect(Person[:dave]).to eq(1)
43
+ it "should return id column value when passing symbol name to [] method" do
44
+ expect(Person[:dave]).to eq(1)
45
+ end
44
46
  end
45
47
 
46
- it "should return array for select helpers from to_select" do
47
- expect(Person.to_select).to eq([['Dave', 1], ['John', 2]])
48
+ context '#to_select' do
49
+ it "should return array for select helpers" do
50
+ expect(Person.to_select).to eq([['Dave', 1], ['John', 2]])
51
+ end
52
+
53
+ it "should return sorted array from order value for select helpers when an order is specified" do
54
+ expect(SortedPerson.to_select).to eq([['John', 2], ['Dave', 1]])
55
+ end
48
56
  end
49
57
 
50
- it "should return sorted array from order value for select helpers from to_select when an order is specified" do
51
- expect(SortedPerson.to_select).to eq([['John', 2], ['Dave', 1]])
58
+ context '#meta' do
59
+ it "should return record attributes hash without id and name columns" do
60
+ expect(Person.meta(1)).to eq(Person.find(1).attributes.except('id', 'first_name'))
61
+ end
52
62
  end
53
63
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc1
4
+ version: 1.0.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-05 00:00:00.000000000 Z
11
+ date: 2019-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  version: 1.3.1
85
85
  requirements: []
86
86
  rubyforge_project:
87
- rubygems_version: 2.5.1
87
+ rubygems_version: 2.7.6.2
88
88
  signing_key:
89
89
  specification_version: 4
90
90
  summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes