Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to be able to use enum accessor constant with same name as top-level constant #161

Merged

Conversation

yujideveloper
Copy link
Contributor

@yujideveloper yujideveloper commented Mar 29, 2018

Overview

I fixed to be able to use enum accessor constant with same name as top-level constant such as JSON.

Before & After Behavior

Before (v2.0.0)

irb(main):001:0> class FooFormat < ActiveHash::Base
irb(main):002:1>   include ActiveHash::Enum
irb(main):003:1>   self.data = [
irb(main):004:2*     { type: 'JSON'},
irb(main):005:2*     { type: 'YAML'},
irb(main):006:2*     { type: 'XML'}
irb(main):007:2>   ]
irb(main):008:1>   enum_accessor :type
irb(main):009:1> end
Traceback (most recent call last):
       16: from /home/yuji_developer/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
       15: from /home/yuji_developer/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/bundler/cli.rb:27:in `dispatch'
       14: from /home/yuji_developer/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
       13: from /home/yuji_developer/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
       12: from /home/yuji_developer/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
       11: from /home/yuji_developer/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/bundler/cli.rb:455:in `console'
       10: from /home/yuji_developer/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/bundler/cli/console.rb:19:in `run'
        9: from (irb):1
        8: from (irb):8:in `<class:FooFormat>'
        7: from /home/yuji_developer/oss/active_hash/lib/enum/enum.rb:14:in `enum_accessor'
        6: from /home/yuji_developer/oss/active_hash/lib/active_hash/base.rb:402:in `reload'
        5: from /home/yuji_developer/oss/active_hash/lib/active_hash/base.rb:79:in `data='
        4: from /home/yuji_developer/oss/active_hash/lib/active_hash/base.rb:79:in `each'
        3: from /home/yuji_developer/oss/active_hash/lib/active_hash/base.rb:80:in `block in data='
        2: from /home/yuji_developer/oss/active_hash/lib/enum/enum.rb:19:in `insert'
        1: from /home/yuji_developer/oss/active_hash/lib/enum/enum.rb:39:in `set_constant'
ActiveHash::Enum::DuplicateEnumAccessor (JSON already defined for Class)
irb(main):010:0>

After

irb(main):001:0> class FooFormat < ActiveHash::Base
irb(main):002:1>   include ActiveHash::Enum
irb(main):003:1>   self.data = [
irb(main):004:2*     { type: 'JSON'},
irb(main):005:2*     { type: 'YAML'},
irb(main):006:2*     { type: 'XML'}
irb(main):007:2>   ]
irb(main):008:1>   enum_accessor :type
irb(main):009:1> end
=> false
irb(main):010:0> FooFormat::JSON
=> #<FooFormat:0x00007fffdc6c0618 @attributes={:type=>"JSON", :id=>1}>
irb(main):011:0> FooFormat.constants
=> [:YAML, :JSON, :XML, :DuplicateEnumAccessor, :Methods, :ClassMethods]
irb(main):012:0>

@yujideveloper yujideveloper force-pushed the bugfix/fix-duplicate-enum-accessor branch from 36a718c to b730df3 Compare March 29, 2018 14:34
@yujideveloper
Copy link
Contributor Author

Fix CI at PR #162

@yujideveloper yujideveloper force-pushed the bugfix/fix-duplicate-enum-accessor branch from b730df3 to d97db61 Compare April 3, 2018 03:39
@syguer
Copy link
Collaborator

syguer commented Apr 4, 2018

Thank you 😄

@syguer syguer merged commit 55a0e98 into active-hash:master Apr 4, 2018
@yujideveloper yujideveloper deleted the bugfix/fix-duplicate-enum-accessor branch April 4, 2018 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants