class Praat::MetaObject
Attributes
parent[RW]
Public Instance Methods
add_property(name, value)
click to toggle source
Add the property to the object
# 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)
click to toggle source
Append the object to the collection of names
# 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 = {})
click to toggle source
# 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_s()
click to toggle source
# File lib/praat.rb, line 96 def to_s "#{self.inspect}" end
Private Instance Methods
properties()
click to toggle source
# File lib/praat.rb, line 109 def properties instance_variables.reject {|f| f == :@parent}.map do |v| v.to_s.gsub "@", "" end end
sanitize_name(name)
click to toggle source
# File lib/praat.rb, line 102 def sanitize_name name if name == "class" name = "klass" end name.downcase.sub(' ', '_') end