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



154
155
156
157
# File 'lib/sup/util.rb', line 154

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

#bool_reader(*args) ⇒ Object



150
151
152
# File 'lib/sup/util.rb', line 150

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

#bool_writer(*args) ⇒ Object



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

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

#defer_all_other_method_calls_to(obj) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/sup/util.rb', line 159

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sup.rb', line 29

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