Class: Dhall::Record

Inherits:
Expression show all
Includes:
Enumerable
Defined in:
lib/dhall/ast.rb,
lib/dhall/binary.rb,
lib/dhall/normalize.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #as_dhall, #cache_key, #call, #concat, #deep_merge_type, #dhall_eq, #digest, #fusion, #resolve, #to_binary, #to_cbor, #to_proc, #to_s, #|

Class Method Details

.decode(record) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/dhall/binary.rb', line 146

def self.decode(record)
  if record.empty?
    EmptyRecord.new
  else
    new(record: Hash[record.map { |k, v| [k, Dhall.decode(v)] }])
  end
end

.for(record) ⇒ Object



707
708
709
710
711
712
713
# File 'lib/dhall/ast.rb', line 707

def self.for(record)
  if record.empty?
    EmptyRecord.new
  else
    new(record: record)
  end
end

Instance Method Details

#==(other) ⇒ Object



781
782
783
# File 'lib/dhall/ast.rb', line 781

def ==(other)
  other.respond_to?(:record) && record.to_a == other.record.to_a
end

#[](k) ⇒ Object



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

def [](k)
  record[k.to_s]
end

#as_jsonObject



793
794
795
# File 'lib/dhall/ast.rb', line 793

def as_json
  [8, Hash[record.to_a.map { |k, v| [k, v.as_json] }.sort]]
end

#deep_merge(other) ⇒ Object



761
762
763
764
765
766
767
768
# File 'lib/dhall/ast.rb', line 761

def deep_merge(other)
  other = other.as_dhall
  return super unless other.is_a?(Record)

  with(record: Hash[record.merge(other.record) { |_, v1, v2|
    v1.deep_merge(v2)
  }.sort])
end

#dig(*keys) ⇒ Object



749
750
751
752
753
754
755
756
757
758
759
# File 'lib/dhall/ast.rb', line 749

def dig(*keys)
  if keys.empty?
    raise ArgumentError, "wrong number of arguments (given 0, expected 1+)"
  end

  key, *rest = keys.map(&:to_s)
  v = record.fetch(key) { return nil }
  return v if rest.empty?

  v.dig(*rest)
end

#each(&block) ⇒ Object



715
716
717
718
# File 'lib/dhall/ast.rb', line 715

def each(&block)
  record.each(&block)
  self
end

#eql?(other) ⇒ Boolean



785
786
787
# File 'lib/dhall/ast.rb', line 785

def eql?(other)
  self == other
end

#fetch(k, default = nil, &block) ⇒ Object



736
737
738
# File 'lib/dhall/ast.rb', line 736

def fetch(k, default=nil, &block)
  record.fetch(k.to_s, *default, &block)
end

#keysObject



724
725
726
# File 'lib/dhall/ast.rb', line 724

def keys
  record.keys
end

#map(&block) ⇒ Object



777
778
779
# File 'lib/dhall/ast.rb', line 777

def map(&block)
  with(record: Hash[record.map(&block)])
end

#merge(other) ⇒ Object



770
771
772
773
774
775
# File 'lib/dhall/ast.rb', line 770

def merge(other)
  other = other.as_dhall
  return super unless other.is_a?(Record)

  with(record: Hash[record.merge(other.record).sort])
end

#normalizeObject



296
297
298
299
300
# File 'lib/dhall/normalize.rb', line 296

def normalize
  with(record: Hash[
    record.map { |k, v| [k, v.nil? ? v : v.normalize] }.sort
  ])
end

#shift(amount, name, min_index) ⇒ Object



302
303
304
305
306
307
308
# File 'lib/dhall/normalize.rb', line 302

def shift(amount, name, min_index)
  with(record: Hash[
    record.map { |k, v|
      [k, v.nil? ? v : v.shift(amount, name, min_index)]
    }.sort
  ])
end

#slice(*keys) ⇒ Object



740
741
742
743
744
745
746
747
# File 'lib/dhall/ast.rb', line 740

def slice(*keys)
  keys = keys.map(&:to_s)
  if record.respond_to?(:slice)
    self.class.for(record.slice(*keys))
  else
    self.class.for(record.select { |k, _| keys.include?(k) })
  end
end

#substitute(var, with_expr) ⇒ Object



310
311
312
313
314
315
316
# File 'lib/dhall/normalize.rb', line 310

def substitute(var, with_expr)
  with(record: Hash[
    record.map { |k, v|
      [k, v.nil? ? v : v.substitute(var, with_expr)]
    }.sort
  ])
end

#to_hObject



720
721
722
# File 'lib/dhall/ast.rb', line 720

def to_h
  record
end

#valuesObject



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

def values
  record.values
end

#with(attrs) ⇒ Object



789
790
791
# File 'lib/dhall/ast.rb', line 789

def with(attrs)
  self.class.new({ record: record }.merge(attrs))
end