Class: Gitdb::Card

Inherits:
Object
  • Object
show all
Defined in:
lib/gitdb/Card.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Card

Returns a new instance of Card.

[View source]

5
6
7
# File 'lib/gitdb/Card.rb', line 5

def initialize repo
  @repo = repo
end

Class Method Details

.delete(repo, id) ⇒ Object

[View source]

80
81
82
# File 'lib/gitdb/Card.rb', line 80

def self::delete repo, id
  Card.new(repo).access(id).delete
end

.exist?(repo, id) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

9
10
11
# File 'lib/gitdb/Card.rb', line 9

def self::exist? repo, id
  repo.head.target.tree.find { |o| o[:name] == id } unless repo.branches.count == 0
end

.getdata(repo, id) ⇒ Object

[View source]

64
65
66
# File 'lib/gitdb/Card.rb', line 64

def self::getdata repo, id
  Card.new(repo).access(id).getdata
end

.getmeta(repo, id) ⇒ Object

[View source]

72
73
74
# File 'lib/gitdb/Card.rb', line 72

def self::getmeta repo, id
  Card.new(repo).access(id).getmeta
end

.setdata(repo, id, hash) ⇒ Object

[View source]

68
69
70
# File 'lib/gitdb/Card.rb', line 68

def self::setdata repo, id, hash
  Card.new(repo).access(id).setdata hash
end

.setmeta(repo, id, hash) ⇒ Object

[View source]

76
77
78
# File 'lib/gitdb/Card.rb', line 76

def self::setmeta repo, id, hash
  Card.new(repo).access(id).setmeta hash
end

Instance Method Details

#access(id) ⇒ Object

[View source]

49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitdb/Card.rb', line 49

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

#create(uid) ⇒ Object

[View source]

37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gitdb/Card.rb', line 37

def create uid
  # 生成唯一的名片id
  while exist?(@id = Gitil::generate_code(4)) 
  end
  # 记录名片创建者id
  @uid = uid
  # 用默认格式初始化名片可读内容
  @content = format_card @id, @uid
  # 返回Card实例
  self
end

#deleteObject

Notice: 调用delete之后需要先commit, 然后才能继续调用write_to_stage

[View source]

108
109
110
111
# File 'lib/gitdb/Card.rb', line 108

def delete
  @repo.index.read_tree @repo.head.target.tree unless @repo.branches.count == 0
  @repo.index.find { |blob| @repo.index.remove blob[:path] if blob[:path] == @id }
end

#exist?(id) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

13
14
15
# File 'lib/gitdb/Card.rb', line 13

def exist? id
  Card::exist? @repo, id
end

#format_card(id, uid) ⇒ Object

设置名片默认格式 Notice: 每个hash的key为Symbol类型

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitdb/Card.rb', line 19

def format_card id, uid
  {
    meta: {
      id: id,
      owner: uid
    },
    firstname: '',
    lastname: '',
    mobile: [],
    phone: [],
    email: [],
    address: [],
    im: [],
    birthday: '',
    note: ''
  }
end

#getdataObject

[View source]

84
85
86
87
88
# File 'lib/gitdb/Card.rb', line 84

def getdata
  data = @content.clone
  data.delete :meta
  data
end

#getmetaObject

[View source]

99
100
101
# File 'lib/gitdb/Card.rb', line 99

def getmeta
  @content.clone[:meta]
end

#setdata(hash) ⇒ Object

[View source]

90
91
92
93
94
95
96
97
# File 'lib/gitdb/Card.rb', line 90

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
  # 每次对数据的修改会触发一次"写入暂存区"
  write_to_stage @id, JSON.pretty_generate(@content)
end

#setmeta(hash) ⇒ Object

[View source]

103
104
105
# File 'lib/gitdb/Card.rb', line 103

def setmeta hash
  @content[:meta] = hash
end

#write_to_stage(id, content) ⇒ Object

[View source]

113
114
115
116
117
# File 'lib/gitdb/Card.rb', line 113

def write_to_stage id, content
  oid = @repo.write content, :blob
  @repo.index.read_tree @repo.head.target.tree unless @repo.branches.count == 0
  @repo.index.add :path => id, :oid => oid, :mode => 0100644
end