Skip to content

Feature | Object Data Class#5

Merged
bruno-costanzo merged 2 commits intomainfrom
feature/object
Feb 14, 2025
Merged

Feature | Object Data Class#5
bruno-costanzo merged 2 commits intomainfrom
feature/object

Conversation

@bruno-costanzo
Copy link
Contributor

What

Introduces Ruber::Object class that converts nested Hashes into immutable Data objects
Supports nested hashes, arrays, and primitive values
Provides dot notation access with method_missing

Why

Offers clean, type-safe way to handle structured data
Makes working with nested data structures more intuitive
Provides immutability out of the box

@bruno-costanzo bruno-costanzo force-pushed the feature/object branch 4 times, most recently from 5b2bb19 to 0d12016 Compare February 14, 2025 09:28

module Ruber
class Object
attr_reader :data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necesitamos este attr_reader?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originalmente había pensado que podía servir para darle la posibilidad de tener el hash original en el caso de que la persona prefiera eso. Pero no es eso lo que devuelve así que lo voy a volar.

Comment on lines +25 to +29
def method(name)
super unless @data.respond_to?(name)

@data.public_send(name)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necesitamos este método?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, creo que fue un intento que hice para evitar usar el method_missing


result = @data.public_send(name, *args)

result.is_a?(Data) ? self.class.new(result.to_h) : result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me pregunto si esto no debería estar en to_data_object en lugar de acá y que quede toda la lógica de transformación ahí:

...
if obj.is_a?(Hash)
  wrapped = obj.transform_values do |val|
    result = to_data_object(val)
    result.is_a?(Data) ? self.class.new(result.to_h) : result
  end
...
def method_missing(name, *args)
  super unless @data.respond_to?(name)

  @data.public_send(name, *args)
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buena sugerencia!

@bruno-costanzo bruno-costanzo merged commit 062c7c8 into main Feb 14, 2025
1 check passed
@bruno-costanzo bruno-costanzo deleted the feature/object branch February 14, 2025 13:51
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