Class: Gitdb::Card
- Inherits:
-
Object
- Object
- Gitdb::Card
- Defined in:
- lib/gitdb/Card.rb
Constant Summary collapse
- @@lock =
线程锁
Mutex.new
- @@lock_repo =
记录申请资源的repo, 用于判断是否需要锁
[]
Class Method Summary collapse
Instance Method Summary collapse
- #access(id) ⇒ Object
- #add_to_stage(id, content) ⇒ Object
- #create(uid) ⇒ Object
- #delete ⇒ Object
- #exist?(id) ⇒ Boolean
-
#format_card(id, uid) ⇒ Object
设置名片默认格式 Notice: 每个hash的key为Symbol类型.
- #getdata ⇒ Object
- #getmeta ⇒ Object
-
#initialize(repo) ⇒ Card
constructor
A new instance of Card.
- #setdata(hash) ⇒ Object
- #setmeta(hash) ⇒ Object
Constructor Details
#initialize(repo) ⇒ Card
Returns a new instance of Card.
10 11 12 |
# File 'lib/gitdb/Card.rb', line 10 def initialize repo @repo = repo end |
Class Method Details
.exist?(repo, id) ⇒ Boolean
14 15 16 17 |
# File 'lib/gitdb/Card.rb', line 14 def self::exist? repo, id # 检查stage而不是tree nil != repo.index.get(id) end |
Instance Method Details
#access(id) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gitdb/Card.rb', line 73 def access id @id = id o = @repo.head.target.tree.find do |o| o if o[:name] == id end # 找到名片后加载名片数据, 并返回Card实例, 否则返回nil if o != nil @content = JSON.parse @repo.lookup(o[:oid]).content, { symbolize_names: true } @uid = @content[:owner] self else nil end end |
#add_to_stage(id, content) ⇒ Object
115 116 117 118 |
# File 'lib/gitdb/Card.rb', line 115 def add_to_stage id, content oid = @repo.write content, :blob @repo.index.add :path => id, :oid => oid, :mode => 0100644 end |
#create(uid) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gitdb/Card.rb', line 43 def create uid block = Proc.new { # 生成唯一的名片id while exist?(@id = Gitil::generate_code(4)) end # 记录名片创建者id @uid = uid # 用默认格式初始化名片可读内容 @content = format_card @id, @uid # 添加到暂存区 add_to_stage @id, JSON.pretty_generate(@content) } # 如果repo中存在资源申请 if @@lock_repo.include? @repo.workdir # 同步信号量 @@lock.synchronize &block else # 否则将新的repo加入记录 @@lock_repo << @repo.workdir block.call end # 使用结束后清理记录 @@lock_repo.delete @repo.workdir # 返回Card实例 self end |
#delete ⇒ Object
111 112 113 |
# File 'lib/gitdb/Card.rb', line 111 def delete @repo.index.remove @id end |
#exist?(id) ⇒ Boolean
19 20 21 |
# File 'lib/gitdb/Card.rb', line 19 def exist? id Card::exist? @repo, id end |
#format_card(id, uid) ⇒ Object
设置名片默认格式 Notice: 每个hash的key为Symbol类型
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gitdb/Card.rb', line 25 def format_card id, uid { meta: { id: id, owner: uid }, firstname: '', lastname: '', mobile: [], phone: [], email: [], address: [], im: [], birthday: '', note: '' } end |
#getdata ⇒ Object
88 89 90 91 92 |
# File 'lib/gitdb/Card.rb', line 88 def getdata data = @content.clone data.delete :meta data end |
#getmeta ⇒ Object
103 104 105 |
# File 'lib/gitdb/Card.rb', line 103 def @content.clone[:meta] end |
#setdata(hash) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/gitdb/Card.rb', line 94 def setdata hash h = Gitil::data_keys_of_card.map { |key| key.to_sym } & hash.keys h.each do |sym_key| @content[sym_key] = hash[sym_key] end # 每次对数据的修改会触发一次"写入暂存区" add_to_stage @id, JSON.pretty_generate(@content) end |
#setmeta(hash) ⇒ Object
107 108 109 |
# File 'lib/gitdb/Card.rb', line 107 def hash @content[:meta] = hash end |