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



169
170
171
172
# File 'lib/sup/util.rb', line 169

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

#bool_reader(*args) ⇒ Object



165
166
167
# File 'lib/sup/util.rb', line 165

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

#bool_writer(*args) ⇒ Object



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

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

#defer_all_other_method_calls_to(obj) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/sup/util.rb', line 174

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
42
43
44
# File 'lib/sup.rb', line 25

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

  path = name.gsub(/::/, "/")
  yaml_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

  # Legacy
  Psych.load_tags["!#{Redwood::LEGACY_YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}"] = self
end