Skip to content

Instantly share code, notes, and snippets.

@Ji-Yuhang
Ji-Yuhang / grape_format_with.rb
Created September 21, 2017 02:24
grape custom format
Grape::Entity.format_with :utc do |date|
date.strftime('%Y年%m月%d日 %H:%M') if date
end
Grape::Entity.format_with :date do |date|
date.strftime('%Y年%m月%d日') if date
end
Grape::Entity.format_with :datetime do |date|
date.strftime('%Y年%m月%d日 %H:%M:%S') if date
end
@Ji-Yuhang
Ji-Yuhang / grape_format_with_if.rb
Created September 21, 2017 02:23
grape expose format_with: datetime, if: {type: :full}
module Entities::Admin
class OptionType < Grape::Entity
expose :id, documentation: {type: String, desc: 'ID'}
expose :name, documentation: {type: String, desc: '商品属性名称'}
expose :group_name, documentation: {type: String, desc: '属性群名称'}
expose :required, documentation: {type: Boolean, desc: '是否必填'}
expose :position, documentation: {type: Boolean, desc: '排序号'}
expose :phone_category_id, documentation: {type: String, desc: '商品类别ID'}, if: { type: :full }
expose :values, documentation: {type: String, desc: '商品属性值'}, if: { type: :full }
expose :category_names, documentation: {type: String, desc: '商品属性值'}, if: { type: :full }
@Ji-Yuhang
Ji-Yuhang / color_string.rb
Created July 6, 2016 09:25
ruby colorize string
class String
def black; "\e[30m#{self}\e[0m" end
def red; "\e[31m#{self}\e[0m" end
def green; "\e[32m#{self}\e[0m" end
def brown; "\e[33m#{self}\e[0m" end
def blue; "\e[34m#{self}\e[0m" end
def magenta;"\e[35m#{self}\e[0m" end
def cyan; "\e[36m#{self}\e[0m" end
def gray; "\e[37m#{self}\e[0m" end
end
@Ji-Yuhang
Ji-Yuhang / longest_common_substr.rb
Created June 23, 2016 01:58
longest common substring, 一组字符串中找出最长的相同部分
def longest_common_substr(strings)
shortest = strings.min_by &:length
maxlen = shortest.length
maxlen.downto(0) do |len|
0.upto(maxlen - len) do |start|
substr = shortest[start,len]
return substr if strings.all?{|str| str.include? substr }
end
end
end
@Ji-Yuhang
Ji-Yuhang / ctrlp-ignore.vim
Created May 25, 2016 02:51
ctrlp-still-searches-the-ignored-directory
if has("mac")
let g:ctrlp_user_command = 'gfind %s -type f'
" Sane Ignore For ctrlp
endif
if exists("g:ctrlp_user_command")
unlet g:ctrlp_user_command
endif
set wildignore+=*\\vendor\\**
set wildignore+=*/tmp/*,*.so,*.swp,*.zip " MacOSX/Linux
set wildignore+=*\\tmp\\*,*.swp,*.zip,*.exe " Windows
@Ji-Yuhang
Ji-Yuhang / logger.rb
Created May 24, 2016 07:21
custom rails log by open class: ActiveSupport::Logger::SimpleFormatter
# You can enable this feature by: LOGGER=crazy rails s
# File : config/initializers/logger.rb
if ENV["LOGGER"] == "crazy"
class ActiveSupport::Logger::SimpleFormatter
def call(severity, time, progname, msg)
"#{severity} #{time}: #{progname} #{caller.grep(/phoenix/)} -- : #{msg}\n"
end
end
end