Class: I18n::Keys::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/i18n/keys/index.rb,
lib/i18n/keys/formatter.rb

Defined Under Namespace

Modules: Formatter

Constant Summary collapse

@@formatter =
Formatter::Stdin
@@default_pattern =
'/**/*.{rb,erb}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Index

Returns a new instance of Index.



85
86
87
88
89
90
91
# File 'lib/i18n/keys/index.rb', line 85

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  @name = args.first || :default
  @pattern = options[:pattern] || @@default_pattern
  reset!
  @@formatter.setup(self) if @@formatter
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



74
75
76
# File 'lib/i18n/keys/index.rb', line 74

def name
  @name
end

Class Method Details

.create(*args) ⇒ Object



39
40
41
42
43
# File 'lib/i18n/keys/index.rb', line 39

def create(*args)
  index = new(*args)
  index.update
  index
end

.delete(name) ⇒ Object



57
58
59
# File 'lib/i18n/keys/index.rb', line 57

def delete(name)
  new(name).delete
end

.delete_allObject



61
62
63
# File 'lib/i18n/keys/index.rb', line 61

def delete_all
  FileUtils.rm_r(store_dir) if exists? rescue Errno::ENOENT
end

.exists?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/i18n/keys/index.rb', line 53

def exists?(name = nil)
  name ? File.exists?(filename(name)) : File.exists?(store_dir)
end

.filename(name) ⇒ Object



65
66
67
# File 'lib/i18n/keys/index.rb', line 65

def filename(name)
  store_dir + "/#{name.to_s}.marshal"
end

.load(name) ⇒ Object



45
46
47
# File 'lib/i18n/keys/index.rb', line 45

def load(name)
  File.open(filename(name), 'r') { |f| Marshal.load(f) } if exists?
end

.load_or_create(*args) ⇒ Object



33
34
35
36
37
# File 'lib/i18n/keys/index.rb', line 33

def load_or_create(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  name = args.first || :default
  exists?(name) ? load(name) : create(name, options)
end

.load_or_create_or_init(*args) ⇒ Object



26
27
28
29
30
31
# File 'lib/i18n/keys/index.rb', line 26

def load_or_create_or_init(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  name = TrueClass === args.first ? :default : args.first
  index = name ? load_or_create(name, options) : new(name, options)
  index
end

.mk_dirObject



49
50
51
# File 'lib/i18n/keys/index.rb', line 49

def mk_dir
  FileUtils.mkdir_p(store_dir) unless exists?
end

.store_dirObject



69
70
71
# File 'lib/i18n/keys/index.rb', line 69

def store_dir
  File.expand_path(Keys.meta_dir + '/indizes')
end

Instance Method Details

#build(options = {}) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/i18n/keys/index.rb', line 104

def build(options = {})
  @occurences = find_occurences(options)
  @occurences.each do |occurence|
    @keys << occurence.key
    (@by_key[occurence.key] ||= []) << occurence
  end
  @built = true
end

#built?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/i18n/keys/index.rb', line 100

def built?
  @built
end

#configObject



144
145
146
# File 'lib/i18n/keys/index.rb', line 144

def config
  @config ||= Keys.config['indices'][name] || { }
end

#deleteObject



128
129
130
# File 'lib/i18n/keys/index.rb', line 128

def delete
  FileUtils.rm(filename) if exists? rescue Errno::ENOENT
end

#each(*keys) ⇒ Object



148
149
150
151
# File 'lib/i18n/keys/index.rb', line 148

def each(*keys)
  patterns = key_patterns(keys)
  occurences.each { |occurence| yield(occurence) if key_matches?(occurence.key, patterns) }
end

#exists?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/i18n/keys/index.rb', line 113

def exists?
  File.exists?(filename)
end

#filenameObject



132
133
134
# File 'lib/i18n/keys/index.rb', line 132

def filename
  self.class.filename(name)
end

#filesObject



136
137
138
# File 'lib/i18n/keys/index.rb', line 136

def files
  Dir[Keys.root + pattern]
end

#inject(memo, *keys) ⇒ Object



153
154
155
156
# File 'lib/i18n/keys/index.rb', line 153

def inject(memo, *keys)
  each(*keys) { |occurence| yield(memo, occurence) }
  memo
end

#marshal_dumpObject



163
164
165
166
# File 'lib/i18n/keys/index.rb', line 163

def marshal_dump
  keys = :name, :pattern, :keys, :occurences, :by_key
  keys.inject({ :built => built? }) { |result, key| result[key] = send(key); result }
end

#marshal_load(data) ⇒ Object



168
169
170
171
# File 'lib/i18n/keys/index.rb', line 168

def marshal_load(data)
  keys = :name, :pattern, :keys, :occurences, :by_key, :built
  keys.each { |key| instance_variable_set(:"@#{key}", data[key]) }
end

#patternObject



140
141
142
# File 'lib/i18n/keys/index.rb', line 140

def pattern
  @pattern ||= config['pattern'] || @@default_pattern
end

#replace!(occurence, replacement) ⇒ Object



158
159
160
161
# File 'lib/i18n/keys/index.rb', line 158

def replace!(occurence, replacement)
  occurence.replace!(replacement)
  save if exists?
end

#reset!Object



93
94
95
96
97
98
# File 'lib/i18n/keys/index.rb', line 93

def reset!
  @built = false
  @keys = []
  @occurences = []
  @by_key = {}
end

#saveObject



117
118
119
120
# File 'lib/i18n/keys/index.rb', line 117

def save
  self.class.mk_dir
  File.open(filename, 'w+') { |f| Marshal.dump(self, f) }
end

#update(options = {}) ⇒ Object



122
123
124
125
126
# File 'lib/i18n/keys/index.rb', line 122

def update(options = {})
  reset!
  build(options)
  save
end