Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Decorators 101 Python decorators allow you to modify or extend the behavior of functions and methods without changing their actual code. When you use a Python decorator, you wrap a function with another function, which takes the origin
functools — Higher-order functions and operations on callable objects¶ Source code: Lib/functools.py The functools module is for higher-order functions: functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module. The functools module defines the following functions: @functools.cache(user_function)¶ Simple lightweigh
What is the decorator design pattern? And how can you use this pattern in your Ruby projects? The decorator design pattern helps you enhance an object by adding new capabilities to it without having to change the class. Let’s look at an example! Logging & Performance In this example we are making HTTP requests using a gem like rest-client. Here’s what that looks like: require 'restclient' data = R
By Sihui Huang The decorator pattern is about adding extra features to an existing object. Does that sound like French? No worries. We will come back to this later. Let’s take a look at some waffles first! The genius part about waffles is that they start plain and simple. Because they are plain, almost everything tastes good with them Some common toppings for waffles are strawberries, blueberries,
NOTE This document refers to an experimental stage 2 decorators implementation. Stage 3 decorator support is available since Typescript 5.0. See: Decorators in Typescript 5.0 Introduction With the introduction of Classes in TypeScript and ES6, there now exist certain scenarios that require additional features to support annotating or modifying classes and class members. Decorators provide a way t
『るびま』は、Ruby に関する技術記事はもちろんのこと、Rubyist へのインタビューやエッセイ、その他をお届けするウェブ雑誌です。 Rubyist Magazine について 『Rubyist Magazine』、略して『るびま』は、Rubyist の Rubyist による、Rubyist とそうでない人のためのウェブ雑誌です。 最新号 Rubyist Magazine 0064 号 バックナンバー Rubyist Magazine 0064 号 Rubyist Magazine 0063 号 Rubyist Magazine 0062 号 Kaigi on Rails 特集号 RubyKaigi Takeout 2020 特集号 Rubyist Magazine 0061 号 Rubyist Magazine 0060 号 RubyKaigi 2019 直前特集号 Rubyist
記念すべき「Rails Tips」第1回は、Decorator と Presenter について書きます。 Rails で Decorator/Presenter というと draper や active_decorator などの実績のある有名な gem パッケージが存在していて、それらを導入すれば話は簡単なのですが、本稿ではあえて Rails 標準の機能を用いて Decorator/Presenter を実現する方法を説明します。「車輪の再発明!」と言わないでください。自分で作ってみることによって Ruby や Rails の知識が深まり、様々な応用が利くようになります。実際のところ、そんなに複雑なものではありません。 Decorator とは 「Decorator」はソフトウェアデザインパターンの1つで、継承(inheritance)に代わるクラスの拡張手段です。 具体例で説明しまし
Photo by Flickr: HerryLawford's Photostream DraperはRailsのプレゼンテーション層の役割を担うgemです。 この記事では、Draperを通し、プレゼンテーション層の必要性や使い方を説明します。 動作確認 Ruby 2.2.1 Rails 4.2.0 Draper 1.4.0 目次 0. プレゼンテーション層の必要性 1. Draperのインストール方法 2. Draperの簡単な使い方 3. デコレーターインスタンスの作成 3.1. 単独のオブジェクトのデコレーター 3.2. コレクションの個々のオブジェクトのデコレーター 3.3. コレクション自身のデコレーター 3.4. 関連するオブジェクトのデコレーター 4. デコレータークラスの作成 4.1. デコレーター内でヘルパーメソッドへのアクセス 4.2. デコレーター内でモデルオブジェク
Railsで、モデルの内容をビューに出すときにちょっと加工するみたいな時、そのコードはどこに書けばいいんだ問題。 デザインパターンの一つにDecoratorパターンというのがあって、これを適用させるのが良いらしい。この用途ではactive_decoratorと並んで人気のgem、draperの説明がとてもわかりやすかった。 draperのREADMEで挙げられているのは、Articleという記事のモデルがあり、これの公開状態を表示させる機能を実装するという例だ。これを読み、なぜヘルパーメソッドやモデルに直に実装するんじゃだめなの?という疑問に対する答えを意訳していく。 ヘルパーメソッドを作る場合 ヘルパーメソッドで書くと、こうなる。 # app/helpers/articles_helper.rb def publication_status(article) if article.pub
Draperって何?Draperは、Model/Controller/View/Helperと異なる第4のレイヤを提供するGemです。 この第4のレイヤは、海外では「Presenter」として分類されています。(正確にもう少し細かい分類があるらしいですが割愛♪) このレイヤを作るメリットは、 model/viewに余計なロジックを書かなくてすむ Helperのメソッドが使われている場所が不明という事態を防ぐ 日本ではPresenterレイヤでは、ActiveDecoratorというGemが有名です。ActiveDecoratorもほぼ同じことができます。 Draperの紹介Draperの説明ならRailsCastが一番わかり易かったです(説明は日本語に翻訳されています、動画は英語です)↓ #####286 Draper - RailsCasts 1年前の記事ですので、実際に動かしてみると少
Using decorators in Ruby on Rails has many benefits. Models become less fat, views less complicated, and using the procedural view helpers becomes a thing of the past. When applying decorators to your Rails project, you might be tempted to maintain a 1:1 mapping between models and decorators. All the presentation logic of a Article would go into an ArticleDecorator. While the decorators are small,
Download sample VS 2008 SP1 project - 21.32 KB Contents Introduction Background and Concepts A Classic Example Query Decorators Downloads History Introduction This article series will explore practical uses and applications of design patterns in day to day development practices, using practical examples in C#. The first article (one of hopefully many more to come) will cover the Decorator Pattern.
With ES6 finalized in June, it is time to look forward to ES7. There are some awesome proposals that you should get excited about, including: Object.observe, async functions, and the new bind operator (<this>::<fn>). The future of javascript is even more exciting with the announcement of Web Assembly. All this mixed with V8, Node and Tessel, and we are seeing Javascript slowly begin to take over t
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く