Skip to content
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

Necessity of handling decisions in separate way by using map #18

Closed
plucury opened this issue Feb 5, 2013 · 5 comments
Closed

Necessity of handling decisions in separate way by using map #18

plucury opened this issue Feb 5, 2013 · 5 comments
Milestone

Comments

@plucury
Copy link
Contributor

plucury commented Feb 5, 2013

In my development, I often write decisions for different request-method as following code

decision? (fn [ctx]
                   (case (get-in ctx [:request :request-method ])
                      :get do-get-decision
                      :put do-put-decision))

So I want to make this easier like this:

decision? {:get do-get-decision :put do-put decision}

I don't know if there is anyone wants this too.

@ordnungswidrig
Copy link
Member

That's not in the spirit of how liberator works. Can you please give an example where you need this decision?

@plucury
Copy link
Contributor Author

plucury commented Feb 6, 2013

For example, I hava a resource named "User". And I want to use POST method to create a user than use PUT method to modify it. In additional, everyone can create user. But only user itself can modify it. So I have following code:

authorized? (fn [ctx] (case (get-in ctx [:request :request-method]) :post true :put (check-auth?)))

@ordnungswidrig
Copy link
Member

That is a good point! I like your idea of using a mapping of methods to decision functions. Another approach would by to provide a higher order function like

(decision? (by-method :get do-get :any:do-default))

with

(defn by-method [& kvs]
    (fn [ctx] 
        (let [m (apply hash-map kvs)
              method (get-in ctx [:request :request-method])]
           (if-let [d (or (get m method) (get m :any))] (d ctx)))))

@plucury
Copy link
Contributor Author

plucury commented Feb 8, 2013

I think it's a good solution. Hoping for seeing it in version 1.0.
Maybe there can be a determination in by-method for the situation that either method-key or :any is not found?

@plucury
Copy link
Contributor Author

plucury commented Mar 21, 2013

Pull Request #23

@plucury plucury closed this as completed Mar 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants