Comments (5)
Also, I get an idea to introduce data_source
. I see it like this (it's just a draft to explain my idea):
module Post
class BaseDataSource
include Kan::DataSource
role(:base) { |_user, _| true }
register(:index) { |_user, repo| repo }
end
class AdminDataSource
include Kan::DataSource
role(:admin) { |user, _| user.admin? }
register(:index) { |_user, repo| repo.exclude(draft: true) }
end
end
app = Kan::Application.new(
data_sources: { post: [Post::BaseDataSource.new, Post::AdminDataSource.new] }
)
KanImport = Dry::AutoInject(app)
class Service
include KanImport[data_source: 'data_source.post.index']
include Import['post_repo']
def call(user, params)
source = data_source.call(user, post_repo) #=> relation with condition
# ...
end
end
In this case, you can get a specific repository or data source based on your role and use it for getting or setting data. WDYT?
from kan.
Also, I get an idea to introduce
data_source
. I see it like this (it's just a draft to explain my idea):module Post class BaseDataSource include Kan::DataSource role(:base) { |_user, _| true } register(:index) { |_user, repo| repo } end class AdminDataSource include Kan::DataSource role(:admin) { |user, _| user.admin? } register(:index) { |_user, repo| repo.exclude(draft: true) } end endapp = Kan::Application.new( data_sources: { post: [Post::BaseDataSource.new, Post::AdminDataSource.new] } ) KanImport = Dry::AutoInject(app)class Service include KanImport[data_source: 'data_source.post.index'] include Import['post_repo'] def call(user, params) source = data_source.call(user, post_repo) #=> relation with condition # ... end endIn this case, you can get a specific repository or data source based on your role and use it for getting or setting data. WDYT?
I really dig this pattern.
from kan.
hey! I don't have any register_scope
methods right now and I'm not sure that this logic should be in the core (but I can imagine a scope
plugin for kan).
If you have a simple example with the repository and just one call (user.admin?
) I can suggest to use the simplest logic:
class PostRepository
def all_for_user(user)
if user.admin?
self
else
exclude(draft: true)
end
end
end
But if you want to use resource
idea with kan I think you can do it in this way:
module Post
class BaseResources
include Kan::Abilities
role(:base) { |_user, _| true }
register(:index) { |_user, repo| repo }
end
class AdminResources
include Kan::Abilities
role(:admin) { |user, _| user.admin? }
register(:index) { |_user, repo| repo.exclude(draft: true) }
end
end
resources = Kan::Application.new(
post: [Post::BaseResources.new, Post::AdminResources.new]
)
ResourcesImport = Dry::AutoInject(resources)
class Service
include ResourcesImport[resource: 'post.index']
include Import['post_repo']
def call(user, params)
resource.call(user, post_repo) #=> relation with condition
# ...
end
end
from kan.
@davydovanton we are looking at using the above approach, although we are unsure of who this would be used with something like rom-rb
/ hanami-model
. As we currently only return data back from our repository methods and do not leak the relation, e.g. we only return a boolean, single entity, or a collection of entities.
from kan.
After giving this some thought, I see 3 separate solutions if you are using the repository pattern:
- You return the repository method e.g.
register(:index) { |_user| :exclude_draft }
- you return a set of conditions in a data structure that the repository method can understand e.g.
register(:index) { |_user| { draft: false } }
Additionally wrapping this up in a Dry::Monad::Result
could give you the ability to check success or failure, and on success retrieve the conditions / method e.g.
register(:index) { |user| ( Success( { draft: false } ) }
from kan.
Related Issues (20)
- Integrate kan object with dry-container namespaces
- Update README with testing guide
- Question: rspec matchers for testing? HOT 7
- Allow to use role classes, not only blocks HOT 1
- Add metainformation to kan role objects
- Question: replace documentation from readme to separate place
- Docs: How to use kan with frameworks
- Add optional support for ALFA_(XACML)
- Update user documentation
- Role detector ability
- Caching abilities and roles
- Allow to setup notification callback for all abilities
- Metainformation for roles
- Aliases for abilities
- PoC: plugins HOT 1
- Explain motivation on home page
- Make GDPR compliance as a plugin
- Allow to use instrumentation for getting more information about calls
- Syntax errors in the readme HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kan.