Module: FiniteMachine::Threadable::ClassMethods
- Includes:
- InstanceMethods
- Defined in:
- lib/finite_machine/threadable.rb
Instance Method Summary collapse
-
#attr_threadsafe(*attrs) ⇒ nil
Defines threadsafe attributes for a class.
Methods included from InstanceMethods
Instance Method Details
#attr_threadsafe(*attrs) ⇒ nil
Defines threadsafe attributes for a class
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/finite_machine/threadable.rb', line 56 def attr_threadsafe(*attrs) opts = attrs.last.is_a?(::Hash) ? attrs.pop : {} default = opts.fetch(:default, nil) attrs.flatten.each do |attr| class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{attr}(*args) value = args.shift if value self.#{attr} = value elsif instance_variables.include?(:@#{attr}) sync_shared { @#{attr} } elsif #{!default.nil?} sync_shared { instance_variable_set(:@#{attr}, #{default}) } end end alias_method '#{attr}?', '#{attr}' def #{attr}=(value) sync_exclusive { @#{attr} = value } end RUBY_EVAL end end |