class Outline
Constants
- VERSION
Attributes
data[R]
parent[R]
Public Class Methods
new(opts={}, &blk)
click to toggle source
# File lib/outline.rb, line 32 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[:data].nil? || opts[:data].respond_to?(:to_hash) || opts[:data].respond_to?(:to_h) opts = opts.to_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 @methods = [] instance_eval(&blk) unless blk.nil? end
Public Instance Methods
each() { |k, v| ... }
click to toggle source
# File lib/outline.rb, line 86 def each to_h.each { |k, v| yield(k, v) } end
method_missing(meth, *args, &blk)
click to toggle source
# File lib/outline.rb, line 46 def method_missing(meth, *args, &blk) meth = meth.to_s.gsub(/=$/, '').to_sym if meth =~ /=$/ unless @methods.include?(meth) @methods << meth meta_def(meth) do |*values, &blk| block_given, values_given = !blk.nil?, !values.empty? @data ||= {} if !block_given && !values_given @data[meth] = Outline.new(parent: self) unless @data.has_key?(meth) @data[meth] elsif block_given && values_given data = values.delete_at(-1) if values.last.respond_to?(:to_hash) || values.last.respond_to?(:to_h) data = { value: values.length == 1 ? values.first : values }.merge!(data || {}) @data[meth] = Outline.new(parent: self, data: data, &blk) elsif !block_given && values_given data = values.delete_at(-1) if values.last.respond_to?(:to_hash) || values.last.respond_to?(:to_h) if data.nil? @data[meth] = values.length == 1 ? values.first : values else data = { value: values.length == 1 ? values.first : values }.merge!(data || {}) unless values.empty? @data[meth] = Outline.new(parent: self, data: data) end elsif block_given && !values_given @data[meth] = Outline.new(parent: self, &blk) end end meta_def("#{meth}=") { |value| __send__(meth, value) } end __send__(meth, *args, &blk) end
to_h()
click to toggle source
# File lib/outline.rb, line 90 def to_h @data ||= {} @data.each_with_object({}) do |(key, value), memo| memo[key] = value.respond_to?(:to_h) ? value.to_h : value end end