This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import("fmt") | |
type Student struct { | |
id int64 | |
name string | |
} | |
func main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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'] } | |
} | |
}, | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
@obj ||= Object.new | |
def self.obj | |
@obj | |
end | |
def self.inherited(subclass) | |
puts 'Hello inherited' | |
puts "self: #{self}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder