Class: Logaling::GlossarySources::GlossaryYamlSource
- Inherits:
-
Base
- Object
- Base
- Logaling::GlossarySources::GlossaryYamlSource
show all
- Defined in:
- lib/logaling/glossary_sources/glossary_yaml_source.rb
Instance Attribute Summary
Attributes inherited from Base
#glossary, #source_path
Instance Method Summary
collapse
Methods inherited from Base
#absolute_path, #eql?, #hash, #initialize, #mtime
Instance Method Details
#add(source_term, target_term, note) ⇒ Object
36
37
38
39
40
41
42
43
44
|
# File 'lib/logaling/glossary_sources/glossary_yaml_source.rb', line 36
def add(source_term, target_term, note)
initialize_source unless File.exist?(absolute_path)
glossary_source = self.load
glossary_source << build_term(source_term, target_term, note)
dump_glossary_source(glossary_source)
rescue
raise Logaling::GlossaryNotFound
end
|
#delete(source_term, target_term) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/logaling/glossary_sources/glossary_yaml_source.rb', line 59
def delete(source_term, target_term)
raise Logaling::GlossaryNotFound unless File.exist?(absolute_path)
glossary_source = self.load
target_index = find_term_index(glossary_source, source_term, target_term)
unless target_index
raise Logaling::TermError, "Can't found term '#{source_term} #{target_term}' in '#{@glossary.name}'" unless target_index
end
glossary_source.delete_at(target_index)
dump_glossary_source(glossary_source)
end
|
#delete_all(source_term, force = false) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/logaling/glossary_sources/glossary_yaml_source.rb', line 72
def delete_all(source_term, force=false)
raise Logaling::GlossaryNotFound unless File.exist?(absolute_path)
glossary_source = self.load
delete_candidates = target_terms(glossary_source, source_term)
if delete_candidates.empty?
raise Logaling::TermError, "Can't found term '#{source_term} in '#{@glossary.name}'"
end
if delete_candidates.size == 1 || force
glossary_source.delete_if{|term| term['source_term'] == source_term }
dump_glossary_source(glossary_source)
else
raise Logaling::TermError, "There are duplicate terms in glossary.\n" +
"If you really want to delete, please put `loga delete [SOURCE_TERM] --force`\n" +
" or `loga delete [SOURCE_TERM] [TARGET_TERM]`"
end
end
|
#initialize_source ⇒ Object
91
92
93
|
# File 'lib/logaling/glossary_sources/glossary_yaml_source.rb', line 91
def initialize_source
dump_glossary_source([])
end
|
#load ⇒ Object
30
31
32
33
34
|
# File 'lib/logaling/glossary_sources/glossary_yaml_source.rb', line 30
def load
YAML::load_file(absolute_path) || []
rescue TypeError
[]
end
|
#update(source_term, target_term, new_target_term, note) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/logaling/glossary_sources/glossary_yaml_source.rb', line 46
def update(source_term, target_term, new_target_term, note)
raise Logaling::GlossaryNotFound unless File.exist?(absolute_path)
glossary_source = self.load
target_index = find_term_index(glossary_source, source_term, target_term)
if target_index
glossary_source[target_index] = rebuild_term(glossary_source[target_index], source_term, new_target_term, note)
dump_glossary_source(glossary_source)
else
raise Logaling::TermError, "Can't found term '#{source_term}: #{target_term}' in '#{@glossary.name}'"
end
end
|