Class: Gitdb::Card
- Inherits:
-
Object
- Object
- Gitdb::Card
- Defined in:
- lib/gitdb/Card.rb
Constant Summary collapse
- @@lock =
线程锁
false
Class Method Summary collapse
- .delete(repo, id) ⇒ Object
- .exist?(repo, id) ⇒ Boolean
- .getdata(repo, id) ⇒ Object
- .getmeta(repo, id) ⇒ Object
- .setdata(repo, id, hash) ⇒ Object
- .setmeta(repo, id, hash) ⇒ Object
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.
8 9 10 |
# File 'lib/gitdb/Card.rb', line 8 def initialize repo @repo = repo end |
Class Method Details
.delete(repo, id) ⇒ Object
97 98 99 |
# File 'lib/gitdb/Card.rb', line 97 def self::delete repo, id Card.new(repo).access(id).delete end |
.exist?(repo, id) ⇒ Boolean
12 13 14 15 |
# File 'lib/gitdb/Card.rb', line 12 def self::exist? repo, id # 检查stage而不是tree nil != repo.index.get(id) end |
.getdata(repo, id) ⇒ Object
81 82 83 |
# File 'lib/gitdb/Card.rb', line 81 def self::getdata repo, id Card.new(repo).access(id).getdata end |
.getmeta(repo, id) ⇒ Object
89 90 91 |
# File 'lib/gitdb/Card.rb', line 89 def self:: repo, id Card.new(repo).access(id). end |
Instance Method Details
#access(id) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gitdb/Card.rb', line 66 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
128 129 130 131 |
# File 'lib/gitdb/Card.rb', line 128 def add_to_stage id, content oid = @repo.write content, :blob @repo.index.add :path => id, :oid => oid, :mode => 0100644 end |
#create(uid) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gitdb/Card.rb', line 41 def create uid # 等待资源可用 loop do break unless @@lock end # 锁定资源 @@lock = true # 生成唯一的名片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) # 释放资源 @@lock = false # 返回Card实例 self end |
#delete ⇒ Object
124 125 126 |
# File 'lib/gitdb/Card.rb', line 124 def delete @repo.index.remove @id end |
#exist?(id) ⇒ Boolean
17 18 19 |
# File 'lib/gitdb/Card.rb', line 17 def exist? id Card::exist? @repo, id end |
#format_card(id, uid) ⇒ Object
设置名片默认格式Notice: 每个hash的key为Symbol类型
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gitdb/Card.rb', line 23 def format_card id, uid { meta: { id: id, owner: uid }, firstname: '', lastname: '', mobile: [], phone: [], email: [], address: [], im: [], birthday: '', note: '' } end |
#getdata ⇒ Object
101 102 103 104 105 |
# File 'lib/gitdb/Card.rb', line 101 def getdata data = @content.clone data.delete :meta data end |
#getmeta ⇒ Object
116 117 118 |
# File 'lib/gitdb/Card.rb', line 116 def @content.clone[:meta] end |
#setdata(hash) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/gitdb/Card.rb', line 107 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
120 121 122 |
# File 'lib/gitdb/Card.rb', line 120 def hash @content[:meta] = hash end |