Class: Module

Inherits:
Object show all
Defined in:
lib/sup.rb,
lib/sup/util.rb

Instance Method Summary collapse

Instance Method Details

#bool_accessor(*args) ⇒ Object



128
129
130
131
# File 'lib/sup/util.rb', line 128

def bool_accessor *args
  bool_reader(*args)
  bool_writer(*args)
end

#bool_reader(*args) ⇒ Object



124
125
126
# File 'lib/sup/util.rb', line 124

def bool_reader *args
  args.each { |sym| class_eval %{ def #{sym}?; @#{sym}; end } }
end

#bool_writer(*args) ⇒ Object



127
# File 'lib/sup/util.rb', line 127

def bool_writer *args; attr_writer(*args); end

#defer_all_other_method_calls_to(obj) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/sup/util.rb', line 133

def defer_all_other_method_calls_to obj
  class_eval %{
    def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end
    def respond_to?(m, include_private = false)
      @#{obj}.respond_to?(m, include_private)
    end
  }
end

#yaml_properties(*props) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sup.rb', line 23

def yaml_properties *props
  props = props.map { |p| p.to_s }
  vars = props.map { |p| "@#{p}" }
  klass = self
  path = klass.name.gsub(/::/, "/")

  klass.instance_eval do
    define_method(:to_yaml_properties) { vars }
    define_method(:to_yaml_type) { "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" }
  end

  YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val|
    klass.new(*props.map { |p| val[p] })
  end
end