Class: Praat::MetaObject

Inherits:
Object
  • Object
show all
Defined in:
lib/praat.rb,
lib/praat_textgrid.rb

Direct Known Subclasses

Intervals, Item, Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



55
56
57
# File 'lib/praat.rb', line 55

def parent
  @parent
end

Instance Method Details

#add_property(name, value) ⇒ Object

Add the property to the object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/praat.rb', line 77

def add_property name, value
  # Convert it to snake-case
  name = sanitize_name name.to_s

  # Add the attr_accessor if it doesn't exist
  unless self.respond_to? "#{name}"
    self.class.class_exec(name) do |n|
      attr_accessor n.to_sym
    end
  end

  if value.respond_to? :parent=
    value.parent = self
  end

  # Set the attribute to the value
  self.send("#{name}=", value)
end

#add_to_collection(name, object) ⇒ Object

Append the object to the collection of names



72
73
74
# File 'lib/praat.rb', line 72

def add_to_collection name, object
  instance_variable_get("@#{name}s").instance_exec(object) { |o| self << o }
end

#to_json(opts = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/praat.rb', line 57

def to_json opts = {}
  properties.inject Hash.new do |h, v|
    property = instance_variable_get "@#{v}"
    if property.is_a? MetaCollection
      h[v] = property.map {|f| JSON.parse(f.to_json(opts))}
    elsif property.is_a? MetaObject
      h[v] = JSON.parse(property.to_json(opts))
    else
      h[v] = property
    end
    h
  end.to_json(opts)
end

#to_sObject



96
97
98
# File 'lib/praat.rb', line 96

def to_s
  "#{self.inspect}" 
end