class Noid::Rails::Minter::File
A file based minter. This is a simple case.
Attributes
statefile[R]
Public Class Methods
new(template = default_template, statefile = default_statefile)
click to toggle source
Calls superclass method
Noid::Rails::Minter::Base::new
# File lib/noid/rails/minter/file.rb, line 12 def initialize(template = default_template, statefile = default_statefile) @statefile = statefile super(template) end
Public Instance Methods
default_statefile()
click to toggle source
# File lib/noid/rails/minter/file.rb, line 17 def default_statefile Noid::Rails.config.statefile end
read()
click to toggle source
# File lib/noid/rails/minter/file.rb, line 21 def read with_file do |f| state_for(f) end end
write!(minter)
click to toggle source
# File lib/noid/rails/minter/file.rb, line 27 def write!(minter) with_file do |f| # Wipe prior contents so the new state can be written from the beginning of the file f.truncate(0) f.write(Marshal.dump(minter.dump)) end end
Protected Instance Methods
next_id()
click to toggle source
rubocop:enable Security/MarshalLoad
# File lib/noid/rails/minter/file.rb, line 54 def next_id state = read state[:template] &&= state[:template].to_s minter = ::Noid::Minter.new(state) # minter w/in the minter, lives only for an instant id = minter.mint write!(minter) id end
state_for(io_object)
click to toggle source
rubocop:disable Security/MarshalLoad
# File lib/noid/rails/minter/file.rb, line 47 def state_for(io_object) Marshal.load(io_object.read) rescue TypeError, ArgumentError { template: template } end
with_file() { |f| ... }
click to toggle source
# File lib/noid/rails/minter/file.rb, line 37 def with_file ::File.open(statefile, 'a+b', 0o644) do |f| f.flock(::File::LOCK_EX) # Files opened in append mode seek to end of file f.rewind yield f end end