Skip to content

Conversation

@qnighy
Copy link
Collaborator

@qnighy qnighy commented May 20, 2021

Why

This is a PR for the long-awaited features: conditional dependencies and subdependency mapping/passthrough. In short, computed fields can now accept subdeps.

conditional dependencies

Load foo only when use_foo is specified.

dependency foo: -> (subdeps) { subdeps.normalized[:use_foo].any? }
computed def bar; end

subdependency mapping

Load foo with subdeps specified in foospec.

dependency foo: -> (subdeps) { subdeps.normalized[:foospec] }
computed def bar; end

subdependency passthrough

Delegate subdeps as-is.

dependency foo: -> (subdeps) { subdeps }
computed def bar; end

What

To support conditional dependencies, the following changes are made to the interpretation of subdeps:

  • A callable object will be evaluated if it's in a field's dependency list. If it returns an array, it's flattened.
  • subdeps must have at least one truthy value. Otherwise the dependency is considered disabled.

To maintain compatibility and intuition, the normalization process is also changed:

  • :foo or [:foo] will be normalized into { foo: [true] } (previously { foo: [] }).
  • { foo: [] } will be normalized into { foo: [true] } (previously { foo: [] }).

The values nil, false, true are filtered out in the following cases:

  • Before passed to a loader (defined by define_loader or define_primary_loader).
  • Before normalization in -> (subdeps) { subdeps.normalized ... }. Note that the subdeps parameter itself keeps these values as-is.

@codecov
Copy link

codecov bot commented May 20, 2021

Codecov Report

Merging #25 (445026d) into master (cb7ff0c) will increase coverage by 0.62%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #25      +/-   ##
==========================================
+ Coverage   94.29%   94.92%   +0.62%     
==========================================
  Files           4        4              
  Lines         228      256      +28     
  Branches       41       48       +7     
==========================================
+ Hits          215      243      +28     
  Misses          7        7              
  Partials        6        6              
Impacted Files Coverage Δ
lib/computed_model/plan.rb 100.00% <ø> (ø)
lib/computed_model.rb 100.00% <100.00%> (ø)
lib/computed_model/dep_graph.rb 100.00% <100.00%> (ø)
lib/computed_model/model.rb 88.49% <100.00%> (+0.53%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cb7ff0c...445026d. Read the comment docs.

@qnighy qnighy merged commit d59b0d3 into master May 20, 2021
@qnighy qnighy deleted the qnighy/conditionals-and-subdeps branch May 20, 2021 04:10
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