senotrusov-ruby-xml-mapper 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ruby-xml-mapper/file_cache.rb +46 -0
- data/lib/ruby-xml-mapper/file_cache_item.rb +48 -0
- data/lib/ruby-xml-mapper.rb +11 -2
- metadata +4 -2
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
# Copyright 2008-2009 Stanislav Senotrusov <senotrusov@gmail.com>
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
class RubyXmlMapper::FileCache
|
17
|
+
def initialize
|
18
|
+
@items = {}
|
19
|
+
@mutex = Mutex.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def get filename, &block
|
23
|
+
@mutex.synchronize do
|
24
|
+
@items[filename] ||= RubyXmlMapper::FileCacheItem.new(filename)
|
25
|
+
end
|
26
|
+
|
27
|
+
@items[filename].get &block
|
28
|
+
end
|
29
|
+
|
30
|
+
def any_changes?
|
31
|
+
!no_changes?
|
32
|
+
end
|
33
|
+
|
34
|
+
def no_changes?
|
35
|
+
@mutex.synchronize do
|
36
|
+
@items.all? { |filename, item| item.no_changes? }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def clear
|
41
|
+
@mutex.synchronize do
|
42
|
+
@items.clear
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
# Copyright 2008-2009 Stanislav Senotrusov <senotrusov@gmail.com>
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
|
17
|
+
class RubyXmlMapper::FileCacheItem
|
18
|
+
def initialize filename
|
19
|
+
@filename = filename
|
20
|
+
@mutex = Mutex.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def get
|
24
|
+
raise(RubyXmlMapper::CircularReference, "in `#{@filename}'") if @mutex.locked?
|
25
|
+
|
26
|
+
@mutex.synchronize do
|
27
|
+
mtime = File.mtime(@filename)
|
28
|
+
|
29
|
+
if @data && @mtime == mtime
|
30
|
+
return @data
|
31
|
+
|
32
|
+
elsif block_given?
|
33
|
+
@mtime = mtime
|
34
|
+
return (@data = yield)
|
35
|
+
|
36
|
+
else
|
37
|
+
return @data
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def no_changes?
|
43
|
+
@mutex.synchronize do
|
44
|
+
File.mtime(@filename) == @mtime
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/lib/ruby-xml-mapper.rb
CHANGED
@@ -23,9 +23,16 @@
|
|
23
23
|
|
24
24
|
require 'libxml'
|
25
25
|
|
26
|
+
module RubyXmlMapper
|
27
|
+
class CircularReference < StandardError; end
|
28
|
+
end
|
29
|
+
|
30
|
+
require 'ruby-xml-mapper/file_cache_item'
|
31
|
+
require 'ruby-xml-mapper/file_cache'
|
32
|
+
|
26
33
|
module RubyXmlMapper
|
27
34
|
mattr_accessor :file_cache
|
28
|
-
self.file_cache =
|
35
|
+
self.file_cache = RubyXmlMapper::FileCache.new
|
29
36
|
|
30
37
|
def self.included model
|
31
38
|
model.extend RubyXmlMapper::RubyXmlMapperClassMethods
|
@@ -154,7 +161,9 @@ module RubyXmlMapper
|
|
154
161
|
end
|
155
162
|
|
156
163
|
def new_from_xml_file file_name
|
157
|
-
RubyXmlMapper.file_cache
|
164
|
+
RubyXmlMapper.file_cache.get(file_name) do
|
165
|
+
new_from_xml_node(parse_xml_file(file_name))
|
166
|
+
end
|
158
167
|
end
|
159
168
|
|
160
169
|
def new_from_xml_node node
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: senotrusov-ruby-xml-mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stanislav Senotrusov
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -45,6 +45,8 @@ files:
|
|
45
45
|
- README
|
46
46
|
- LICENSE
|
47
47
|
- lib/ruby-xml-mapper
|
48
|
+
- lib/ruby-xml-mapper/file_cache_item.rb
|
49
|
+
- lib/ruby-xml-mapper/file_cache.rb
|
48
50
|
- lib/ruby-xml-mapper/basic_containers.rb
|
49
51
|
- lib/ruby-xml-mapper.rb
|
50
52
|
- test/rss.rb
|