Last active
November 27, 2015 00:58
-
-
Save ivan-leschinsky/a78aae9cd40ce3d298d8 to your computer and use it in GitHub Desktop.
How to add values to enumeration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SomeEnumeration < EnumerateIt::Base | |
| associate_values :one, :two | |
| end | |
| class SomeEnumeration < EnumerateIt::Base | |
| # To add only new values | |
| def self.add_values(*args) | |
| values_hash = (keys + args).each_with_object({}) { |v, h| h[v] = v.to_s } | |
| register_enumeration normalize_enumeration(values_hash) | |
| args.each { |value_name| define_enumeration_constant value_name, value_name.to_s } | |
| end | |
| add_values :three | |
| end | |
| SomeEnumeration.list #=> ['one', 'two', 'three'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A < EnumerateIt::Base | |
| associate_values :a, :b, :c | |
| end | |
| class B < EnumerateIt::Base | |
| associate_values *A.enumeration.keys, :d, :e, :f | |
| end | |
| B.list #=> [:a, :b, :c, :d, :e, :f] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on gem 'enumerate_it', '~> 1.2.0'