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)


642
643
644
645
646
647
# File 'lib/hx.rb', line 642

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



663
664
665
# File 'lib/hx.rb', line 663

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

#to_liquid(*args) ⇒ Object



667
668
669
# File 'lib/hx.rb', line 667

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

#to_sObject



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

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

#to_yaml(*args) ⇒ Object



659
660
661
# File 'lib/hx.rb', line 659

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