Skip to content

Instantly share code, notes, and snippets.

class Foo(object):
def __init__(self, key):
self.key = key
def __get__(self, obj, type):
if int(obj.__dict__.get(self.key, 0)) > 90:
return 'Great'
else:
return 'Shit'
@noel9999
noel9999 / func_with_point.go
Created June 29, 2018 09:42
go 指標變數操作即使不加 * ,似乎也可以直接運作...
package main
import("fmt")
type Student struct {
id int64
name string
}
func main() {
@noel9999
noel9999 / multiple_api_request_in_a_action_with_fiber_example.rb
Last active June 6, 2018 10:36
一個 controller 的 action 需要去戳外部 api 三次的邏輯需求,使用 fiber 整理邏輯
require 'fiber'
Result = Struct.new :success, :payload, :status do
def self.create(args = {})
args = { success: true, payload: {}, status: :ok }.merge(args.symbolize_keys)
new *args.values_at(:success, :payload, :status)
end
def success?
!!success
@noel9999
noel9999 / multiple_api_request_in_a_action_example.rb
Last active June 6, 2018 10:34
一個 controller 的 action 需要去戳外部 api 三次的邏輯需求
class Webhook::CustomersController
# 下列的 action 會有三個步驟,每個步驟都會戳一次 api 然後檢查結果,如果失敗就要 render 結果並退出,成功的話就把資料繼續丟下去給後面的邏輯做
# 這樣的寫法很直覺的流水帳寫下去,但容易把邏輯弄得很肥大,也許之後會難以閱讀
# 然後思考一下發現裡面其實有某種邏輯的循環,但因為下一輪的循環依賴於前者,無法事先知道,所以嘗試使用 fiber 來處理
# 可參考版本:https://gist.github.com/noel9999/172946f174e144c2d48e33535fc5d376
def create
token = get_access_token(params[:merchant_id])
client = OpenApiClient.set(access_token: token).customers
customer = client.get("/#{params[:resource][:_id]}").json_body
mobile_phone, confirmed_at = customer.symbolize_keys.values_at(:mobile_phone, :confirmed_at)
@noel9999
noel9999 / example.rb
Created October 19, 2017 06:41
惱人的動態字串
def tcat_delivery_list
@tcat_delivery_list or begin
delivery_option = DeliveryOption.valid.find_by(owner_type: 'Merchant', owner_id: merchant.id, region_type: 'tw_tcat')
return @tcat_delivery_list = TcatDeliveryList.new('tw_tcat', nil) if delivery_option.blank?
contract_code = delivery_option.config_data['contract_code'].tap { |code| raise "DeliveryOption #{delivery_option.id} has no contract_code" if code.blank? }
@tcat_delivery_list = TcatDeliveryList.new('tw_tcat', contract_code)
end
end
def tcat_pay_delivery_list
@noel9999
noel9999 / aggregate_to_join_and_query.rb
Last active October 26, 2017 03:27
slow aggregate mongo query...
# Order has many order_deliveries
order_deliveries = OrderDelivery.collection.aggregate([
{
"$match" => {
created_at: { '$gte' => Date.new(2017,10,1), '$lt' => Date.new(2017,10,7) },
delivery_status: { '$in' => ['processing', 'pending', 'arrived'] }
}
},
{
@noel9999
noel9999 / shit_code_example.rb
Last active August 25, 2017 09:11
打破一堆原則的不良示範
def index
@page = params.fetch("page",1).to_i
@offset = params.fetch("offset",0).to_i
@limit = params.fetch("limit",24).to_i
@search_fields = params["search_fields"]
# Order status
@status_filter = params.fetch("status_filter","all")
@status_filter = "all" if @status_filter.blank?
@noel9999
noel9999 / es6_generator_example.js
Created July 26, 2017 04:42
es6 generator weird demo
var yieldValueBox = [];
var nextValueBox = []
function * god () {
for (var i = 0; i < 5; i++) {
a = yield ({index: i, time: (new Date()).getTime()})
yieldValueBox.push(a)
console.log('Next passed: ', a);
}
}
var g = god();
@noel9999
noel9999 / class_instance_variable_example.rb
Last active July 19, 2017 03:47
Class Instance Variable example
class A
@obj ||= Object.new
def self.obj
@obj
end
def self.inherited(subclass)
puts 'Hello inherited'
puts "self: #{self}"
module ErrorReporter
extend Forwardable
@name = 'ErrorReporter Class Instance Variable'
@sl_api_client ||= SlApi.api_client
@sl_api_client.public_methods(false).each do |method|
namespace_method = "sl_api_#{method}"
def_delegator @sl_api_client, method, namespace_method