Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/rdoba/yaml.rb,
lib/rdoba/common.rb
Instance Method Summary collapse
- #apply_opts(opts) ⇒ Object
-
#co(method, *args) ⇒ Object
calls any method.
- #parse_opts(opts) ⇒ Object
- #to_sym ⇒ Object
- #to_yml(o = {}) ⇒ Object
- #xor(val1) ⇒ Object
Instance Method Details
#apply_opts(opts) ⇒ Object
33 34 35 36 37 |
# File 'lib/rdoba/common.rb', line 33 def apply_opts(opts) parse_opts(opts).each do |x,y| self.instance_variable_set("@#{x}".to_sym, y) end end |
#co(method, *args) ⇒ Object
calls any method
10 11 12 |
# File 'lib/rdoba/common.rb', line 10 def co(method, *args) #calls any method eval "#{method}(*args)" end |
#parse_opts(opts) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rdoba/common.rb', line 18 def parse_opts(opts) v = {} opts.each do |opt| case opt.class.to_s.to_sym when :Hash opt.each do |x,y| v[x] = y end when :Array opt.each do |x| v[x] = true end when :Symbol v[opt] = true end end v end |
#to_sym ⇒ Object
14 15 16 |
# File 'lib/rdoba/common.rb', line 14 def to_sym to_s.to_sym end |
#to_yml(o = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rdoba/yaml.rb', line 6 def to_yml( o = {} ) level = o[:level] || 0 res = '' res += '---' if level == 0 res += case self.class.to_sym when :Hash rs = '' self.keys.sort do |x,y| (ix, iy) = o[:order] ? [ o[:order].index(x), o[:order].index(y) ] : [ nil, nil ] (ix and iy) ? ix <=> iy : (ix ? -1 : (iy ? 1 : x <=> y)) end.each do |key| value = self[key] rs += "\n" + ' ' * level * 2 + key.to_yml( { :level => level + 1, :order => o[:order] } ) rs += ': ' + value.to_yml( { :level => level + 1, :order => o[:order] } ) end rs.empty? and "{}" or rs when :Array rs = '' self.each do |value| rs += "\n" + ' ' * level * 2 + '- ' + value.to_yml( { :level => level + 1, :order => o[:order] } ) end rs.empty? and "[]" or rs when :Fixnum self.to_s when :String if self =~ /^["'\-:!#={}\[\]~]/ or self =~ /\s+$/ or self =~ /:\s/ if self.count("'") < self.count('"') "'#{self.gsub("'","\\'")}'" else "\"#{self.gsub('"','\"')}\"" end else self end when :NilClass '' else $stderr.puts "Unsupported class #{self.class} to export to yml"; '' end res end |
#xor(val1) ⇒ Object
5 6 7 8 |
# File 'lib/rdoba/common.rb', line 5 def xor(val1) val0 = (not not self) ((val0) and (not val1)) or ((not val0) and (val1)) end |