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



131
132
133
134
# File 'lib/sup/util.rb', line 131

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

#bool_reader(*args) ⇒ Object



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

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

#bool_writer(*args) ⇒ Object



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

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

#defer_all_other_method_calls_to(obj) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/sup/util.rb', line 136

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sup.rb', line 25

def yaml_properties *props
  props = props.map { |p| p.to_s }

  path = name.gsub(/::/, "/")
  yaml_tag "tag:#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}"

  define_method :init_with do |coder|
    initialize(*coder.map.values_at(*props))
  end

  define_method :encode_with do |coder|
    coder.map = props.inject({}) do |hash, key|
      hash[key] = instance_variable_get("@#{key}")
      hash
    end
  end
end