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
Fallback value to OpenStruct delete_field #1409
Conversation
…eting does not exist
I'm not against it, but I'm wondering if there's a particular use case (compared to |
With a bit more thought, given the nature of |
|
Oh, right, my bad. So I guess the equivalent would be
Still, the question remains, what is the use case? |
I currently use it in conjunction with OptionParser does not have a mechanism for required parameter. opts.on("-r", "--required", "This is required with no way to enforce it") { |o| options.required = o } So, what I might currently have to do is this: begin
required = options.delete_field(:required)
rescue NameError
raise MyCustomErrorClass, "You failed to supply required information"
end Since the required = options.delete_field(:required) do
raise MyCustomErrorClass, "You failed to supply required information"
end Also, if there is no field set for some information that you can supply a basic value for, this: foo = options.delete_field(:foo) { 'some foo value' } is much more convenient than this:
or foo = options.foo ||= 'some foo value'
options.delete_field(:foo) I should point out that if a field is set, this change will not affect it, even if the value for the field is nil. person = OpenStruct.new('name' => 'John Smith', 'age' => 70, 'address' => nil)
address = person.delete_field(:address) { '1600 Pennsylvania Ave' }
# => nil So it is not equivalent to the example above, address = person['address'] ||= '1600 Pennsylvania Ave'
# => '1600 Pennsylvania Ave' Obviously there are already ways to deal with the situations I am presenting, but, I feel, with this change it provides a simpler, more efficient means to achieve that result with no negative impact. |
Thanks for the examples. Why do you need to call
And why is options an |
I'm using an options.thing = true
options[:thing] = true I need to use required = options.delete_field(:required) do
raise MyCustomErrorClass, "You failed to supply required information"
end
things = options.delete_field(:thing) { 'value' }
options.table_data ||= "some table data"
# delete more fields/manipulate data...
my_database_thing = MyDatabaseThing.create(options.to_h) I initially expected hash_options = Hash.new
required = hash_options.delete(:required) do
raise MyCustomErrorClass, "You failed to supply required information"
end
# => MyCustomErrorClass: You failed to supply required information
# OpenStruct current implementation
required = options.delete_field(:required) do
raise MyCustomErrorClass, "You failed to supply required information"
end
# => NameError: no field `required' in #<OpenStruct>
foo = hash_options.delete(:foo) { 'some foo value' }
# => "some foo value" Arrays behave in this same fashion. a = Array.new
foo = a.delete(:foo) { 'some foo value' }
# => "some foo value"
foo = a.delete(:foo) do |value|
raise MyCustomErrorClass, "no such value #{value} in array"
end
# => MyCustomErrorClass: no such value foo in array Thus, the proposed change brings |
Now master branch has a required check "check_branch", but I couldn't update your branch to include it. Could you rebase this from master to run it? |
As we cannot merge this anyway without passing the |
@k0kubun Rebased |
I'm 👍 on this, after the small comments have been addressed. |
@marcandre I believe this fits all the requirements now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. A small comment and this is ready to merge.
Thanks @jfrazx Merged. Notes:
Will make a release soon. |
Provide a value to use in the event a field does not exist in your OpenStruct.