Skip to content

Instantly share code, notes, and snippets.

@ivan-leschinsky
Last active November 27, 2015 00:58
Show Gist options
  • Select an option

  • Save ivan-leschinsky/a78aae9cd40ce3d298d8 to your computer and use it in GitHub Desktop.

Select an option

Save ivan-leschinsky/a78aae9cd40ce3d298d8 to your computer and use it in GitHub Desktop.
How to add values to enumeration
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']
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]
@ivan-leschinsky
Copy link
Author

Based on gem 'enumerate_it', '~> 1.2.0'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment