Class: Oozby::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/oozby/element.rb

Overview

Represent an oozby element

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Element

Returns a new instance of Element.



5
6
7
8
# File 'lib/oozby/element.rb', line 5

def initialize hash
  @data = hash
  @siblings = nil #parent_array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &proc) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/oozby/element.rb', line 47

def method_missing name, *args, &proc
  if @data.respond_to? name
    @data.send(name, *args, &proc)
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/oozby/element.rb', line 3

def data
  @data
end

#siblingsObject (readonly)

Returns the value of attribute siblings.



3
4
5
# File 'lib/oozby/element.rb', line 3

def siblings
  @siblings
end

Instance Method Details

#>(other) ⇒ Object

include the next thing as a child of this thing



11
12
13
14
15
# File 'lib/oozby/element.rb', line 11

def > other
  raise "Can only combine an Oozby::Element to another Oozby::Element" unless other.respond_to? :to_oozby_element
  other.to_oozby_element.abduct self.children
  other
end

#abduct(into) ⇒ Object

add this element to the end of supplied array



18
19
20
21
22
# File 'lib/oozby/element.rb', line 18

def abduct into
  @siblings.delete self if @siblings # remove from current location
  @siblings = into # our new location is here
  into.push self # append ourselves to the new parent
end

#indexObject



24
25
26
# File 'lib/oozby/element.rb', line 24

def index
  @siblings.index(self)
end

#to_hObject



61
62
63
# File 'lib/oozby/element.rb', line 61

def to_h
  @data
end

#to_oozby_elementObject



65
66
67
# File 'lib/oozby/element.rb', line 65

def to_oozby_element
  self
end

#to_sObject



55
56
57
58
59
# File 'lib/oozby/element.rb', line 55

def to_s
  x = args.map { |x| x.inspect }
  named_args.each { |name, value| x.push "#{name}: #{value.inspect}"}
  "#{method}(#{x.join(', ')})"
end