Module: RSS::BaseTrackBackModel

Included in:
TrackBackModel10, TrackBackModel20
Defined in:
lib/rss/trackback.rb

Constant Summary collapse

ELEMENTS =
%w(ping about)

Instance Method Summary collapse

Instance Method Details

#append_features(klass) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rss/trackback.rb', line 30

def append_features(klass)
  super

  unless klass.class == Module
    klass.module_eval {include TrackBackUtils}

    klass.install_must_call_validator(TRACKBACK_PREFIX, TRACKBACK_URI)
    %w(ping).each do |name|
      var_name = "#{TRACKBACK_PREFIX}_#{name}"
      klass_name = "TrackBack#{Utils.to_class_name(name)}"
      klass.install_have_child_element(name, TRACKBACK_URI, "?", var_name)
      klass.module_eval(<<-EOC, __FILE__, __LINE__)
        remove_method :#{var_name}
        def #{var_name}
          @#{var_name} and @#{var_name}.value
        end

        remove_method :#{var_name}=
        def #{var_name}=(value)
          @#{var_name} = Utils.new_with_value_if_need(#{klass_name}, value)
        end
      EOC
    end

    [%w(about s)].each do |name, postfix|
      var_name = "#{TRACKBACK_PREFIX}_#{name}"
      klass_name = "TrackBack#{Utils.to_class_name(name)}"
      klass.install_have_children_element(name, TRACKBACK_URI, "*",
                                          var_name)
      klass.module_eval(<<-EOC, __FILE__, __LINE__)
        remove_method :#{var_name}
        def #{var_name}(*args)
          if args.empty?
            @#{var_name}.first and @#{var_name}.first.value
          else
            ret = @#{var_name}.__send__("[]", *args)
            if ret.is_a?(Array)
              ret.collect {|x| x.value}
            else
              ret.value
            end
          end
        end

        remove_method :#{var_name}=
        remove_method :set_#{var_name}
        def #{var_name}=(*args)
          if args.size == 1
            item = Utils.new_with_value_if_need(#{klass_name}, args[0])
            @#{var_name}.push(item)
          else
            new_val = args.last
            if new_val.is_a?(Array)
              new_val = new_value.collect do |val|
                Utils.new_with_value_if_need(#{klass_name}, val)
              end
            else
              new_val = Utils.new_with_value_if_need(#{klass_name}, new_val)
            end
            @#{var_name}.__send__("[]=", *(args[0..-2] + [new_val]))
          end
        end
        alias set_#{var_name} #{var_name}=
      EOC
    end
  end
end