Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dux/aspect
Simple helper that reads attributes assigned to ruby class methods, similar like python function decorators.
https://github.com/dux/aspect
Last synced: about 2 months ago
JSON representation
Simple helper that reads attributes assigned to ruby class methods, similar like python function decorators.
- Host: GitHub
- URL: https://github.com/dux/aspect
- Owner: dux
- Created: 2014-02-23T02:05:54.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-03-01T08:36:54.000Z (almost 11 years ago)
- Last Synced: 2023-03-24T05:21:29.820Z (almost 2 years ago)
- Language: Ruby
- Homepage:
- Size: 113 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Rails.aspects
### About
Simple helper that reads attributes assigned to ruby methods, similar to python function decorators.
If you have file called root_controller.rb
class RootController < ApplicationController
#= foo: bar
#= get
def index
render template: '/root'
end
endif you execute
p RootController.aspects
You wil get this hash
{"index":{"get":true,"foo":"bar"}}### Example 1: Rails rotues
This can be useful if you for example want to have defined routes inside a rails controller, similar like in sinatra.rb
in routes.rb you can define something like this
#= get
def index
...#= post
def action
...App::Application.routes.draw do
aspect_routes UsersController
...
in module app/lib/aspected_routes.db add meanning to aspect_routes method (view source for complete code)
class ActionDispatch::Routing::Mapper
def aspect_routes(controller)
for key, val in "#{controller.to_s.classify.pluralize}Controller".constantize.aspects
if val[:get] && key == 'show'
get "#{controller}/:id" => "#{controller}#show"
elsif val[:post]
...### Example 2: Wrapper roules for methods
class UsersApi < DefaultApi
#= max: 50ms
#= local
def index
render template: '/root'
end
end
Can be parsed to
* log all requests that take longer than 50ms to finish
* allow only local requests to api call
### AuthorDino Reic / @dux / 2014