
Security News
Rspack Introduces Rslint, a TypeScript-First Linter Written in Go
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
h1. Decor
Define multiple representations of an object.
"Read the documentation":http://empl.us/decor/ or see an example scenario and usage below.
h2. Scenario
You have an API. It returns provides access to a User resource. This resource:
create_table do |t| t.string :screen_name t.string :state, :default => "active" t.string :role, :default => "user" end class User < ActiveRecord::Base STATES = ["active", "inactive"] ROLES = ["user", "admin"] end
At one point, you had just defined booleans for @active@ and @admin@, but you have plans to expand the states and roles of a User. However, you already provide an API that you do not wish to break.
How do you transition your code and your data over and not break backwards compatibility? How do you do this modularly and in an easily testable way without making your API endpoints (controllers et al) heavy?
How about:
class User < ActiveRecord::Base include Decor STATES = ["active", "inactive"] ROLES = ["user", "admin"] version "v1" do def active state == "active" end def admin role == "admin" end def as_json(options = {}) super(options.merge(:only => [:screen_name], :methods => [:active, :admin])) end end version "v2" do def as_json(*args) super(options.merge(:only => [:screen_name, :state, :role])) end end end
Then, our endpoint:
get '/api/:version/users/:id' do @user = User.find(params[:id]) @user.for(version).to_json end
You can also include external resources providing some contextual influence over how your resources choose to represent themselves:
class User < ActiveRecord::Base include Decor version "v3" do def screen_name "%s (%s)" % [super, organization.name] end end end get '/api/:version/organizations/:org_id/users/:id' do @organization = Organization.find(params[:org_id]) @user = User.find(params[:id]).for(version, :organization => @organization) @user.to_json end
h2. Example Usage
Defining your class and versions:
class Resource < Struct.new(:name, :value) include Decor version "v1" do # a computed value specific to this version def computed value * 10 end end version "v2" do # overloaded name def name super.reverse end # different computed value for this version def computed value * 100 end end def unversioned "yes" end end
Using the versioned instance:
resource = resource.for(version)
h2. Contributing
Feel free to open "Issues":https://github.com/mtodd/decor/issues or fork and create "Pull Requests":https://github.com/mtodd/decor/pulls.
The minimum for contribution is a failing spec. If you can at least convey what the problem is or the feature you'd like to add in a failing spec, I will be much more motivated to fix the issue or add the feature.
If you'd like to fix the bug or implement the feature yourself, feel free to! Please document and test your code thoroughly and in similar fashion to what already exists.
h2. Copyright and License
The MIT License
Copyright (c) 2010 Matt Todd.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Unknown package
We found that decor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
Security News
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.