Skip to content

Commit

Permalink
Attribute accessors hash likely
Browse files Browse the repository at this point in the history
  • Loading branch information
ermolaev committed Jul 12, 2024
1 parent 2749691 commit 9395719
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Style/SingleLineMethods:
Style/StringLiterals:
EnforcedStyle: single_quotes

Rails/Delegate:
Enabled: false

AllCops:
NewCops: enable
TargetRubyVersion: 2.7
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@ gem 'active_dry_form'

### Under the hood ActiveDryForm uses [dry-validation](https://dry-rb.org/gems/dry-validation), [dry-monads](https://dry-rb.org/gems/dry-monads)

#### Initialize form
```ruby
form = ProductForm.new(record: Product.find(1), params: { product: { title: 'n', price: 120 } })
ProductForm.new(params: { title: 'n', price: '120' })
ProductForm.new(params: { product: { title: 'n', price: '120' } })
ProductForm.new(record: Product.find(params[:id]), params:)
```

#### Attribute accessors
```ruby
form.title # => 'n'
form.price # => '120'
form.price = '119' # => '119'
form[:price] # => '119'
form[:price] = '121' # => '121'
```

#### Methods
```ruby
form.validate # => checks field validity
form.validator # => #<Dry::Validation::Result{:title=>"n", :price=>120, errors={:title=>["minimum length 2"]}...>
form.valid? # => false
Expand All @@ -21,13 +36,14 @@ form.errors # => {:title=>["minimum length 2"]}
form.base_errors = []
form.errors_full_messages # => ['Cannot be less than 2 words']
form.record # => #<Product:0x00007f05c27106c8 id: 1, title: 'name', price: 100, description: 'product'>
# form.data - is hash, casted to types, after validate
form.data # => {:title=>"n", :price=>120}
form.data[:price] # => 120
form.price # => '120'
form.title # => 'n'
form.update # Failure(:invalid_form)
```



Methods `form.update` and `form.create` return [Result monad](https://dry-rb.org/gems/dry-monads/1.3/result/)

```ruby
Expand Down
12 changes: 8 additions & 4 deletions lib/active_dry_form/base_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def self.wrap(object)
form
end

def [](k)
attributes[k]
end

def []=(k, v)
attributes[k] = _deep_transform_values_in_params!(v)
end

def self.define_methods
const_set :NESTED_FORM_KEYS, []

Expand All @@ -148,10 +156,6 @@ def self.define_methods
attributes[key] = _deep_transform_values_in_params!(v)
end

define_method :'[]=' do |k, v|
attributes[k] = _deep_transform_values_in_params!(v)
end

sub_klass =
if value[:properties] || value.dig(:items, :properties)
Class.new(BaseForm).tap do |klass|
Expand Down
7 changes: 7 additions & 0 deletions spec/active_dry_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
expect(form.name).to eq 'Vasya'
end

it 'attribute accessors' do
form = UserForm.new(params: { name: 'Vasya' })
expect(form[:name]).to eq 'Vasya'
expect { form[:second_name] = 'Petrov' }.to change(form, :second_name).to('Petrov')
expect(form[:second_name]).to eq 'Petrov'
end

it 'set hash with unknown key' do
described_class.config.strict_param_keys = false

Expand Down

0 comments on commit 9395719

Please sign in to comment.