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)


671
672
673
674
675
676
# File 'lib/hx.rb', line 671

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



692
693
694
# File 'lib/hx.rb', line 692

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

#to_liquid(*args) ⇒ Object



696
697
698
# File 'lib/hx.rb', line 696

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

#to_sObject



678
679
680
681
682
683
684
685
686
# File 'lib/hx.rb', line 678

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

#to_yaml(*args) ⇒ Object



688
689
690
# File 'lib/hx.rb', line 688

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