Class: Gitdb::Card
- Inherits:
-
Object
- Object
- Gitdb::Card
- Defined in:
- lib/gitdb/Card.rb
Constant Summary collapse
- @@lock =
线程锁
Mutex.new
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
8 9 10 |
# File 'lib/gitdb/Card.rb', line 8 def initialize repo @repo = repo end |
Class Method Details
.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 |
Instance Method Details
#access(id) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gitdb/Card.rb', line 60 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
102 103 104 105 |
# File 'lib/gitdb/Card.rb', line 102 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 |
# File 'lib/gitdb/Card.rb', line 41 def create uid # 同步信号量 @@lock.synchronize { # 生成唯一的名片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) } # 返回Card实例 self end |
#delete ⇒ Object
98 99 100 |
# File 'lib/gitdb/Card.rb', line 98 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
75 76 77 78 79 |
# File 'lib/gitdb/Card.rb', line 75 def getdata data = @content.clone data.delete :meta data end |
#getmeta ⇒ Object
90 91 92 |
# File 'lib/gitdb/Card.rb', line 90 def @content.clone[:meta] end |
#setdata(hash) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/gitdb/Card.rb', line 81 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
94 95 96 |
# File 'lib/gitdb/Card.rb', line 94 def hash @content[:meta] = hash end |