Class: RSS::Maker::Base

Inherits:
Object show all
Extended by:
Utils::InheritedReader
Defined in:
lib/rss/maker/base.rb

Constant Summary

OTHER_ELEMENTS =
[]
NEED_INITIALIZE_VARIABLES =
[]

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Utils::InheritedReader

inherited_array_reader, inherited_hash_reader, inherited_reader

Constructor Details

- (Base) initialize(maker)

A new instance of Base



175
176
177
178
179
# File 'lib/rss/maker/base.rb', line 175

def initialize(maker)
  @maker = maker
  @default_values_are_set = false
  initialize_variables
end

Instance Attribute Details

- (Object) maker (readonly)

Returns the value of attribute maker



174
175
176
# File 'lib/rss/maker/base.rb', line 174

def maker
  @maker
end

Class Method Details

+ (Object) add_need_initialize_variable(variable_name, init_value = nil, &init_block)



34
35
36
37
38
# File 'lib/rss/maker/base.rb', line 34

def add_need_initialize_variable(variable_name, init_value=nil,
                                 &init_block)
  init_value ||= init_block
  self::NEED_INITIALIZE_VARIABLES << [variable_name, init_value]
end

+ (Object) add_other_element(variable_name)



30
31
32
# File 'lib/rss/maker/base.rb', line 30

def add_other_element(variable_name)
  self::OTHER_ELEMENTS << variable_name
end

+ (Object) def_array_element(name, plural = nil, klass_name = nil)



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
# File 'lib/rss/maker/base.rb', line 40

def def_array_element(name, plural=nil, klass_name=nil)
  include Enumerable
  extend Forwardable

  plural ||= "#{name}s"
  klass_name ||= Utils.to_class_name(name)
  def_delegators("@#{plural}", :<<, :[], :[]=, :first, :last)
  def_delegators("@#{plural}", :push, :pop, :shift, :unshift)
  def_delegators("@#{plural}", :each, :size, :empty?, :clear)

  add_need_initialize_variable(plural) {[]}

  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    def new_#{name}
      #{name} = self.class::#{klass_name}.new(@maker)
      @#{plural} << #{name}
      if block_given?
        yield #{name}
      else
        #{name}
      end
    end
    alias new_child new_#{name}

    def to_feed(*args)
      @#{plural}.each do |#{name}|
        #{name}.to_feed(*args)
      end
    end

    def replace(elements)
      @#{plural}.replace(elements.to_a)
    end
  EOC
end

+ (Object) def_classed_element(name, class_name = nil, attribute_name = nil)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rss/maker/base.rb', line 94

def def_classed_element(name, class_name=nil, attribute_name=nil)
  def_classed_element_without_accessor(name, class_name)
  if attribute_name
    module_eval(<<-EOC, __FILE__, __LINE__ + 1)
      def #{name}
        if block_given?
          yield(@#{name})
        else
          @#{name}.#{attribute_name}
        end
      end

      def #{name}=(new_value)
        @#{name}.#{attribute_name} = new_value
      end
    EOC
  else
    attr_reader name
  end
end

+ (Object) def_classed_element_without_accessor(name, class_name = nil)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rss/maker/base.rb', line 76

def def_classed_element_without_accessor(name, class_name=nil)
  class_name ||= Utils.to_class_name(name)
  add_other_element(name)
  add_need_initialize_variable(name) do |object|
    object.send("make_#{name}")
  end
  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    private
    def setup_#{name}(feed, current)
      @#{name}.to_feed(feed, current)
    end

    def make_#{name}
      self.class::#{class_name}.new(@maker)
    end
  EOC
end

+ (Object) def_classed_elements(name, attribute, plural_class_name = nil, plural_name = nil, new_name = nil)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rss/maker/base.rb', line 115

def def_classed_elements(name, attribute, plural_class_name=nil,
                         plural_name=nil, new_name=nil)
  plural_name ||= "#{name}s"
  new_name ||= name
  def_classed_element(plural_name, plural_class_name)
  local_variable_name = "_#{name}"
  new_value_variable_name = "new_value"
  additional_setup_code = nil
  if block_given?
    additional_setup_code = yield(local_variable_name,
                                  new_value_variable_name)
  end
  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    def #{name}
      #{local_variable_name} = #{plural_name}.first
      #{local_variable_name} ? #{local_variable_name}.#{attribute} : nil
    end

    def #{name}=(#{new_value_variable_name})
      #{local_variable_name} =
        #{plural_name}.first || #{plural_name}.new_#{new_name}
      #{additional_setup_code}
      #{local_variable_name}.#{attribute} = #{new_value_variable_name}
    end
  EOC
end

+ (Object) def_csv_element(name, type = nil)



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rss/maker/base.rb', line 159

def def_csv_element(name, type=nil)
  def_other_element_without_accessor(name)
  attr_reader(name)
  converter = ""
  if type == :integer
    converter = "{|v| Integer(v)}"
  end
  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    def #{name}=(value)
      @#{name} = Utils::CSV.parse(value)#{converter}
    end
  EOC
end

+ (Object) def_other_element(name)



142
143
144
145
# File 'lib/rss/maker/base.rb', line 142

def def_other_element(name)
  attr_accessor name
  def_other_element_without_accessor(name)
end

+ (Object) def_other_element_without_accessor(name)



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rss/maker/base.rb', line 147

def def_other_element_without_accessor(name)
  add_need_initialize_variable(name)
  add_other_element(name)
  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    def setup_#{name}(feed, current)
      if !@#{name}.nil? and current.respond_to?(:#{name}=)
        current.#{name} = @#{name}
      end
    end
  EOC
end

+ (Object) inherited(subclass)



25
26
27
28
# File 'lib/rss/maker/base.rb', line 25

def inherited(subclass)
  subclass.const_set("OTHER_ELEMENTS", [])
  subclass.const_set("NEED_INITIALIZE_VARIABLES", [])
end

+ (Object) inherited_base



21
22
23
# File 'lib/rss/maker/base.rb', line 21

def inherited_base
  ::RSS::Maker::Base
end

+ (Object) need_initialize_variables



17
18
19
# File 'lib/rss/maker/base.rb', line 17

def need_initialize_variables
  inherited_array_reader("NEED_INITIALIZE_VARIABLES")
end

+ (Object) other_elements



14
15
16
# File 'lib/rss/maker/base.rb', line 14

def other_elements
  inherited_array_reader("OTHER_ELEMENTS")
end

Instance Method Details

- (Boolean) have_required_values?

Returns:

  • (Boolean)


181
182
183
# File 'lib/rss/maker/base.rb', line 181

def have_required_values?
  not_set_required_variables.empty?
end

- (Boolean) variable_is_set?

Returns:

  • (Boolean)


185
186
187
# File 'lib/rss/maker/base.rb', line 185

def variable_is_set?
  variables.any? {|var| not __send__(var).nil?}
end