Class: DataObj

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

Instance Method Summary collapse

Constructor Details

#initializeDataObj

DataObj

Scoped variables for feeding a Liquid parsing operation



695
696
697
# File 'lib/liquidoc.rb', line 695

def initialize
  @data = {"vars" => {}}
end

Instance Method Details

#add_data!(data, scope = "") ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/liquidoc.rb', line 699

def add_data! data, scope=""
  # Merges data into existing scope or creates a new scope
  if scope.empty? # store new object at root of this object
    self.data.merge!data
  else # store new object as a subordinate, named object
    if self.data.key?(scope) # merge/append into existing object
      self.data[scope].merge!data if self.data[scope].is_a? Hash
      self.data[scope] << data if self.data[scope].is_a? Array
    else # create a new key named after the scope
      scoped_hash = { scope => data }
      self.data.merge!scoped_hash
    end
  end
end

#add_payload!(payload) ⇒ Object



714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/liquidoc.rb', line 714

def add_payload! payload
  # Expects an Array of Hashes ([{name=>String, data=>Object},...])
  if payload.size == 1
    # If payload is a single Hash, store it at the root level (no scope)
    self.add_data!(payload[0]['data']) if payload[0]['data'].is_a? Hash
    # Insert arrays into the data. scope, and for backward compatibility, hashes as well
    self.add_data!(payload[0]['data'], "data")
  end
  # For ALL payloads, create a self-named obj scope
  payload.each do |obj|
    self.add_data!(obj['data'], obj['name']) # Insert object under self-named scope
  end
end

#dataObject



728
729
730
# File 'lib/liquidoc.rb', line 728

def data
  @data
end

#remove_scope(scope) ⇒ Object



732
733
734
# File 'lib/liquidoc.rb', line 732

def remove_scope scope
  self.data.delete(scope)
end