Module: Psychgus::Ext::ObjectExt

Defined in:
lib/psychgus/ext/core_ext.rb

Overview

Core extensions to Object.

Instance Method Summary collapse

Instance Method Details

#to_yaml(options = {}) ⇒ String

Convert an Object to YAML.

options can also be a Hash, so can be a drop-in-replacement for Psych.

Examples:

class MyStyler
  include Psychgus::Styler

  def style_sequence(sniffer,node)
    node.style = Psychgus::SEQUENCE_FLOW
  end
end

my_obj = {
  :Foods => {
    :Fruits  => %w(Apple Banana Blueberry Pear),
    :Veggies => %w(Bean Carrot Celery Pea)
}}

puts my_obj.to_yaml(indentation: 5,stylers: MyStyler.new)

# Or, pass in a Hash:
#puts my_obj.to_yaml({:indentation=>5,:stylers=>MyStyler.new})

# Output:
# ---
# :Foods:
#      :Fruits: [Apple, Banana, Blueberry, Pear]
#      :Veggies: [Bean, Carrot, Celery, Pea]

Parameters:

  • options (Hash) (defaults to: {})

    the options (or keyword args) to pass to Psychgus.dump

Returns:

  • (String)

    the YAML generated from this Object

See Also:



54
55
56
57
58
59
60
# File 'lib/psychgus/ext/core_ext.rb', line 54

def to_yaml(options={})
  # NOTE: This method signature must use old-style `options={}` instead of `**options`!
  #       Because some Gems, like `Moneta`, depend on this.

  # Do not use Psych.dump() if no Stylers, because a class might be a Blueberry
  return Psychgus.dump(self,**options)
end