Module: CrazyDoll::Config

Defined in:
lib/crazy_doll/config.rb

Defined Under Namespace

Modules: Backend Classes: Instance

Class Method Summary collapse

Class Method Details

.new(type, *args) ⇒ Object



5
6
7
8
9
10
# File 'lib/crazy_doll/config.rb', line 5

def self.new(type, *args)
  case type
  when /yaml/i then Backend::YAML.new(*args)
  else raise ArgumentError, "no config backend #{type.inspect}"
  end
end

.parse(owner, key, description, value) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/crazy_doll/config.rb', line 12

def self.parse(owner, key, description, value)
  out = setup_class(value.class) unless value.respond_to?(:crazy_doll_setuped?) and value.to_yaml('class_setuped?') == 'YES SIR !'
  return value if value.crazy_doll_setuped?
  value.owner       = owner
  value.key         = key
  value.description = description
  value
end

.setup_class(klass) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crazy_doll/config.rb', line 21

def self.setup_class(klass)
  klass.class_eval do
    def crazy_doll_setuped?
      @owner and @key and @description ? true : false
    end

    alias :crazy_doll_setuped :crazy_doll_setuped?

    [['owner', 'to_s'], ['key', 'to_sym'], ['description', 'to_s']].each do |name,m|
      class_eval <<-EVAL
        def #{name}
          @#{name} || nil
        end

        def #{name}=(value)
          raise ArgumentError, "can't redefine #{name}" if defined?(@#{name})
          @#{name} = value.#{m}
        end
      EVAL
    end

    alias :__to_yaml :to_yaml

    def _to_yaml(opts = {})
      return __to_yaml(opts) unless crazy_doll_setuped?
      a, b, c = @owner, @key, @description
      ['@owner', '@key', '@description'].each { |x| remove_instance_variable(x) }
      begin
        return __to_yaml(opts)
      ensure
        @owner, @key, @description = a, b, c
      end
    end

    def to_yaml( opts = {} )
      return 'YES SIR !' if opts == 'class_setuped?'
      return _to_yaml(opts) unless crazy_doll_setuped?
      YAML.quick_emit( self.object_id, opts ) do |out|
        out.map( "tag:crazydoll.org,2010:ConfigVar", "crazydoll.org,2010/ConfigVar" ) do |map|
          map.add('owner',       self.owner      )
          map.add('key',         self.key        )
          map.add('description', self.description)
          map.add('value',       self._to_yaml   )
        end
      end
    end
  end
end