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

Nested hash field defined by external class doesn't work #51

Open
akoltun opened this issue Sep 16, 2016 · 2 comments
Open

Nested hash field defined by external class doesn't work #51

akoltun opened this issue Sep 16, 2016 · 2 comments

Comments

@akoltun
Copy link

akoltun commented Sep 16, 2016

Model = Struct.new(:content)

class Nested < Disposable::Twin
  property :nested_property
end

class Outer < Disposable::Twin
  include Property::Hash
  property :content, field: hash, twin: Nested 
end

Outer.new(Model.new({nested_property: 1}))  # will fail with message: 
# NoMethodError: undefined method `nested_property' for {:nested_property=>1}}:Hash

The reason of failing is that the Property::Hash module includes the following three modules (NestedDefaults, Property::Struct, Hash::Sync) in the nested class by means of the feature mechanism which works only for nested fields defined in the block but not in the separate class.

Since this feature behaviour seems to be correct (in general modules should be explicitly included in the classes) I can see two possible ways to solve an issue:

  • use another dedicated way to include these specific modules into the nested class (in this particular case I see it to be appropriated)
  • explicitly include these modules in the nested class. For now it is just a workaround which uses undocumented functions. In order to become a solution it should be documented and preserved from the unannounced changes but actually it looks ugly:
class Nested < Disposable::Twin
  feature Disposable::Twin::Property::Hash::NestedDefaults
  feature Disposable::Twin::Property::Struct
  feature Disposable::Twin::Property::Hash::Sync

  property :nested_property
end

It's nicer to have just one module that should be included and which will do this work. Something like this:

class Nested < Disposable::Twin
  feature Disposable::Twin::Property::Hashable

  property :nested_property
end
@apotonick
Copy link
Owner

What happens if you do

class Nested < Disposable::Twin
  include Property::Hash
  property :nested_property
end

@akoltun
Copy link
Author

akoltun commented Sep 30, 2016

The same exception:
NoMethodError: undefined methodnested_property' for {:nested_property=>1}}:Hash`

Because Property::Hash itself does not provide methods to deserialise hash into object. But uses feature mechanism to mix-in necessary modules into nested properties. But feature mechanism does work only for nested blocks.

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

No branches or pull requests

2 participants