Class: Hx::LazyContent

Inherits:
Object
  • Object
show all
Defined in:
lib/hx.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ LazyContent

Returns a new instance of LazyContent.

Raises:

  • (ArgumentError)


640
641
642
643
644
645
# File 'lib/hx.rb', line 640

def initialize(&block)
  raise ArgumentError, "No block given" unless block
  @lock = Mutex.new
  @content = nil
  @block = block
end

Instance Method Details

#to_json(*args) ⇒ Object



661
662
663
# File 'lib/hx.rb', line 661

def to_json(*args)
  to_s.to_json(*args)
end

#to_liquid(*args) ⇒ Object



665
666
667
# File 'lib/hx.rb', line 665

def to_liquid(*args)
  to_s.to_liquid(*args)
end

#to_sObject



647
648
649
650
651
652
653
654
655
# File 'lib/hx.rb', line 647

def to_s
  @lock.synchronize do
    if @block
      @content = @block.call
      @block = nil
    end
  end
  @content
end

#to_yaml(*args) ⇒ Object



657
658
659
# File 'lib/hx.rb', line 657

def to_yaml(*args)
  to_s.to_yaml(*args)
end