Class: Milkode::MilkodeYaml

Inherits:
Object
  • Object
show all
Defined in:
lib/milkode/cdstk/milkode_yaml.rb

Constant Summary collapse

MILKODE_YAML_VERSION =
'0.2'
EMPTY_YAML =
<<EOF
---
version: '#{MILKODE_YAML_VERSION}'
contents: []
EOF

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = nil) ⇒ MilkodeYaml

Returns a new instance of MilkodeYaml.



24
25
26
27
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 24

def initialize(str = nil)
  @data = YAML.load(str || EMPTY_YAML)
  @contents = parse_contents
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



22
23
24
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 22

def contents
  @contents
end

Instance Method Details

#add(package) ⇒ Object

パッケージを追加



38
39
40
41
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 38

def add(package)
  @contents.push package
  update_contents
end

#dumpObject



29
30
31
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 29

def dump
  YAML.dump(@data)
end

#find_dir(directory) ⇒ Object

ディレクトリ名が同じパッケージを検索



68
69
70
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 68

def find_dir(directory)
  @contents.find {|v| v.directory == directory}
end

#find_name(name) ⇒ Object

名前が同じパッケージを検索



58
59
60
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 58

def find_name(name)
  @contents.find {|v| v.same_name?(name)}
end

#match_all(keyword) ⇒ Object

指定キーワードにマッチする全てのパッケージを返す



63
64
65
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 63

def match_all(keyword)
  @contents.find_all {|p| p.name.include? keyword }
end

#migrateObject

マイグレーション



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 82

def migrate
  if (version != MILKODE_YAML_VERSION)
    # バージョン番号
    @data['version'] = MILKODE_YAML_VERSION

    # パッケージ
    contents.each{|v| v.migrate}

    # migrateが起きた
    true
  else
    false
  end
end

#package_root(dir) ⇒ Object

指定ディレクトリの所属するパッケージのルートディレクトリを得る。見つからない場合はnilを返す。



74
75
76
77
78
79
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 74

def package_root(dir)
  nd = Util::normalize_filename dir
  @contents.find do |v|
    v if nd =~ /^#{v.directory}/
  end
end

#remove(package) ⇒ Object

パッケージを削除



52
53
54
55
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 52

def remove(package)
  @contents.delete(package)
  update_contents
end

#update(package) ⇒ Object

同名パッケージの内容を置き換え



44
45
46
47
48
49
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 44

def update(package)
  i = @contents.find_index {|v| v.same_name?(package.name) }
  raise unless i
  @contents[i] = package
  update_contents
end

#versionObject



33
34
35
# File 'lib/milkode/cdstk/milkode_yaml.rb', line 33

def version
  @data['version']
end