Class: Hash

Inherits:
Object show all
Defined in:
lib/spectator/emacs.rb

Instance Method Summary collapse

Instance Method Details

#as_alistObject

Creates and returns a new hash tabke with the same keys and values and tags it to be rendered as an association list by the to_lisp method.

For example, {:a => :b, :x => 1} would be rendered as

  ((a . b) (x . 1))


80
81
82
# File 'lib/spectator/emacs.rb', line 80

def as_alist
  merge(:__render_as => :alist)
end

#as_flat_listObject

Creates and returns a new hash tabke with the same keys and values but tagged to be rendered as a flat list by the to_lisp method.

For example, {:a => :b, :x => 1} would be rendered as

  (a b x 1)


92
93
94
# File 'lib/spectator/emacs.rb', line 92

def as_flat_list
  merge(:__render_as => :flat )
end

#as_plistObject

Creates and returns a new hash table with the same keys and values but tagged to be rendered as a property list by the to_lisp method. The keys must be symbols, and they will be rendered as keywords.

For example, {:a => :b, :x => 1} would be rendered as

  (:a b :x 1)


105
106
107
# File 'lib/spectator/emacs.rb', line 105

def as_plist
  merge(:__render_as => :plist)
end

#pjoin(string_list) ⇒ Object



119
120
121
# File 'lib/spectator/emacs.rb', line 119

def pjoin(string_list)
  "(#{string_list.join ' '})"
end

#rendering_typeObject

Returns a symbol indicating how the hash will be rendered by the to_lisp method. The possible values are :flat, :alist, :plist.



111
112
113
# File 'lib/spectator/emacs.rb', line 111

def rendering_type
  self[:__render_as] or :plist
end

#to_lispObject

Renders the hash as a list, depending on how it has been tagged. If the hash has not been tagged, it will be rendered as a property list, see as_plist.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/spectator/emacs.rb', line 118

def to_lisp
  def pjoin(string_list)
    "(#{string_list.join ' '})"
  end
  h = self.clone
  h.delete(:__render_as)
  case rendering_type
  when :alist
    pjoin(h.map { |k, v| "(#{k.to_lisp} . #{v.to_lisp})" })
  when :flat
    pjoin(h.map { |k, v| "#{k.to_lisp} #{v.to_lisp}" })
  when :plist
    pjoin(h.map { |k, v| "#{k.keyword.to_lisp} #{v.to_lisp}" })
  end
end