Class: Senv::EnvChangeTracker
- Inherits:
- BasicObject
- Defined in:
- lib/senv.rb
Constant Summary collapse
- THREAD_SAFE =
::Monitor.new
Class Method Summary collapse
Instance Method Summary collapse
- #[]=(key, val) ⇒ Object
- #apply(env) ⇒ Object
- #changes ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(env) ⇒ EnvChangeTracker
constructor
A new instance of EnvChangeTracker.
- #method_missing(method, *args, &block) ⇒ Object
- #replace(hash) ⇒ Object
- #store(key, val) ⇒ Object
Constructor Details
#initialize(env) ⇒ EnvChangeTracker
Returns a new instance of EnvChangeTracker.
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/senv.rb', line 549 def initialize(env) @env = env @changes = { :deleted => [], :updated => [], :created => [], } @change_for = proc do |key, val| if @env.has_key?(key) case when val.nil? {:type => :deleted, :info => [key, val]} when val.to_s != @env[key].to_s {:type => :updated, :info => [key, val]} else nil end else {:type => :created, :info => [key, val]} end end @track_change = proc do |key, val| change = @change_for[key, val] if change @changes[change[:type]].push(change[:info]) end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
586 587 588 |
# File 'lib/senv.rb', line 586 def method_missing(method, *args, &block) @env.send(method, *args, &block) end |
Class Method Details
.track(&block) ⇒ Object
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
# File 'lib/senv.rb', line 627 def EnvChangeTracker.track(&block) THREAD_SAFE.synchronize do env = EnvChangeTracker.new(::ENV) ::Object.send(:remove_const, :ENV) ::Object.send(:const_set, :ENV, env) begin block.call env ensure ::Object.send(:remove_const, :ENV) ::Object.send(:const_set, :ENV, env) end end end |
Instance Method Details
#[]=(key, val) ⇒ Object
590 591 592 593 |
# File 'lib/senv.rb', line 590 def []=(key, val) @track_change[key, val] @env[key] = val end |
#apply(env) ⇒ Object
612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/senv.rb', line 612 def apply(env) @changes[:created].each do |k, v| env[k] = v end @changes[:updated].each do |k, v| env[k] = v end @changes[:deleted].each do |k, v| env.delete(k) end @changes end |
#changes ⇒ Object
582 583 584 |
# File 'lib/senv.rb', line 582 def changes @changes end |
#delete(key) ⇒ Object
607 608 609 610 |
# File 'lib/senv.rb', line 607 def delete(key) @track_change[key, nil] @env.delete(key) end |
#replace(hash) ⇒ Object
595 596 597 598 599 600 |
# File 'lib/senv.rb', line 595 def replace(hash) hash.each do |key, val| @track_change[key, val] end @env.replace(hash) end |
#store(key, val) ⇒ Object
602 603 604 605 |
# File 'lib/senv.rb', line 602 def store(key, val) @track_change[key, val] @env.store(key, val) end |