@@ -52,8 +52,9 @@ def self.validate(json)
5252 # initialize above (top_prop_names etc).
5353 BoutiquesDescriptor = Class . new ( RestrictedHash ) do |klass |
5454
55- allowed_keys top_prop_names # 'name', 'author' etc
56- attr_accessor :from_file # not a hash attribute; a file name, for info
55+ allowed_keys top_prop_names # 'name', 'author' etc
56+ attr_accessor :from_file # not a hash attribute; a file name, for info
57+ attr_accessor :mtime_of_file # not a hash attribute, a file timestamp, for caching
5758
5859 Input = Class . new ( RestrictedHash ) { allowed_keys input_prop_names }
5960 OutputFile = Class . new ( RestrictedHash ) { allowed_keys output_prop_names }
@@ -89,18 +90,26 @@ def self.new_from_string(text)
8990
9091 def self . new_from_file ( path )
9192 obj = self . new_from_string ( File . read ( path ) )
92- obj . from_file = path
93+ obj . from_file = path
94+ obj . mtime_of_file = File . mtime ( path )
9395 obj
9496 end
9597
98+ def reload_if_file_timestamp_changed ( )
99+ filepath = self . from_file
100+ return self if filepath . blank? || File . mtime ( filepath ) == self . mtime_of_file
101+ self . class . new_from_file ( filepath )
102+ end
103+
96104 def validate
97105 BoutiquesSupport . validate ( self ) # amazingly, the JSON validator also work with our descriptor class
98106 end
99107
100108 # When dup'ing, also copy the from_file attribute
101109 def dup #:nodoc:
102110 copy = super
103- copy . from_file = self . from_file
111+ copy . from_file = self . from_file
112+ copy . mtime_of_file = self . mtime_of_file
104113 copy
105114 end
106115
0 commit comments