Class: Outline
Defined Under Namespace
Classes: ArgumentWithBlockError
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#initialize(opts = {}, &blk) ⇒ Outline
constructor
A new instance of Outline.
- #inspect ⇒ Object
- #method_missing(meth, *args, &blk) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(opts = {}, &blk) ⇒ Outline
Returns a new instance of Outline.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/outline.rb', line 59 def initialize(opts={}, &blk) raise(TypeError, "opts must respond to :to_hash or be nil") unless opts.nil? || opts.respond_to?(:to_hash) raise(TypeError, "opts[:data] must respond to :to_h or :to_hash or be nil") unless opts.nil? || opts.respond_to?(:to_hash) || opts.respond_to?(:to_h) opts = opts.to_hash # Check to see if incoming data is already an outline # if so, then turn it into a hash data = opts[:data].respond_to?(:to_hash) ? opts[:data].to_hash : opts[:data].to_h unless opts[:data].nil? @parent = opts[:parent] @data = data @data ||= {} ::Kernel.p @data instance_eval(&blk) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/outline.rb', line 79 def method_missing(meth, *args, &blk) meth = meth.to_s.gsub(/=$/, '').to_sym if meth =~ /=$/ (meth) do |value=nil, &blk| block_given, value_given = !blk.nil?, !value.nil? if !block_given && !value_given @data[meth] = Outline.new(parent: self) unless @data.has_key?(meth) @data[meth] elsif block_given && value_given raise ArgumentWithBlockError elsif !block_given && value_given @data[meth] = value elsif block_given && !value_given @data[meth] = Outline.new(parent: self, &blk) end end unless methods.include?(meth) ("#{meth}=") { |value| send(meth, value) } send(meth, *args, &blk) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
57 58 59 |
# File 'lib/outline.rb', line 57 def data @data end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
57 58 59 |
# File 'lib/outline.rb', line 57 def parent @parent end |
Instance Method Details
#inspect ⇒ Object
111 112 113 114 |
# File 'lib/outline.rb', line 111 def inspect "#<Outline:0x#{self.object_id.to_s(16)} @data=#{to_h}>" # "{O}#{to_h}" end |
#to_h ⇒ Object
104 105 106 107 108 109 |
# File 'lib/outline.rb', line 104 def to_h @data.inject({}) do |memo, (key, value)| memo[key] = value#.respond_to?(:to_h) ? value.to_h : value memo end end |