Class: I18n::Keys::Index
- Inherits:
-
Object
- Object
- I18n::Keys::Index
- Includes:
- Enumerable
- Defined in:
- lib/i18n/keys/index.rb,
lib/i18n/keys/formatter.rb
Defined Under Namespace
Modules: Formatter
Constant Summary collapse
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .create(*args) ⇒ Object
- .delete(name) ⇒ Object
- .delete_all ⇒ Object
- .exists?(name = nil) ⇒ Boolean
- .filename(name) ⇒ Object
- .load(name) ⇒ Object
- .load_or_create(*args) ⇒ Object
- .load_or_create_or_init(*args) ⇒ Object
- .mk_dir ⇒ Object
- .store_dir ⇒ Object
Instance Method Summary collapse
- #build(options = {}) ⇒ Object
- #built? ⇒ Boolean
- #config ⇒ Object
- #delete ⇒ Object
- #each(*keys) ⇒ Object
- #exists? ⇒ Boolean
- #filename ⇒ Object
- #files ⇒ Object
-
#initialize(*args) ⇒ Index
constructor
A new instance of Index.
- #inject(memo, *keys) ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(data) ⇒ Object
- #pattern ⇒ Object
- #replace!(occurence, replacement) ⇒ Object
- #reset! ⇒ Object
- #save ⇒ Object
- #update(options = {}) ⇒ Object
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) = args.last.is_a?(Hash) ? args.pop : {} @name = args.first || :default @pattern = [:pattern] || @@default_pattern reset! @@formatter.setup(self) if @@formatter end |
Instance Attribute Details
#name ⇒ Object (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_all ⇒ Object
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
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) = args.last.is_a?(Hash) ? args.pop : {} name = args.first || :default exists?(name) ? load(name) : create(name, ) 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) = args.last.is_a?(Hash) ? args.pop : {} name = TrueClass === args.first ? :default : args.first index = name ? load_or_create(name, ) : new(name, ) index end |
.mk_dir ⇒ Object
49 50 51 |
# File 'lib/i18n/keys/index.rb', line 49 def mk_dir FileUtils.mkdir_p(store_dir) unless exists? end |
.store_dir ⇒ Object
69 70 71 |
# File 'lib/i18n/keys/index.rb', line 69 def store_dir File.(Keys. + '/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( = {}) @occurences = find_occurences() @occurences.each do |occurence| @keys << occurence.key (@by_key[occurence.key] ||= []) << occurence end @built = true end |
#built? ⇒ Boolean
100 101 102 |
# File 'lib/i18n/keys/index.rb', line 100 def built? @built end |
#config ⇒ Object
144 145 146 |
# File 'lib/i18n/keys/index.rb', line 144 def config @config ||= Keys.config['indices'][name] || { } end |
#delete ⇒ Object
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
113 114 115 |
# File 'lib/i18n/keys/index.rb', line 113 def exists? File.exists?(filename) end |
#filename ⇒ Object
132 133 134 |
# File 'lib/i18n/keys/index.rb', line 132 def filename self.class.filename(name) end |
#files ⇒ Object
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_dump ⇒ Object
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 |
#pattern ⇒ Object
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 |
#save ⇒ Object
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( = {}) reset! build() save end |