Class: RSS::Maker::Base

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

Direct Known Subclasses

ChannelBase, ChannelBase::AuthorsBase, ChannelBase::AuthorsBase::AuthorBase, ChannelBase::CategoriesBase, ChannelBase::CategoriesBase::CategoryBase, ChannelBase::CloudBase, ChannelBase::ContributorsBase, ChannelBase::ContributorsBase::ContributorBase, ChannelBase::CopyrightBase, ChannelBase::DescriptionBase, ChannelBase::GeneratorBase, ChannelBase::LinksBase, ChannelBase::LinksBase::LinkBase, ChannelBase::SkipDaysBase, ChannelBase::SkipDaysBase::DayBase, ChannelBase::SkipHoursBase, ChannelBase::SkipHoursBase::HourBase, ChannelBase::TitleBase, ITunesChannelModel::ITunesCategoriesBase, ITunesChannelModel::ITunesCategoriesBase::ITunesCategoryBase, ITunesChannelModel::ITunesImageBase, ITunesChannelModel::ITunesOwnerBase, ITunesItemModel::ITunesDurationBase, ITunesItemModel::ITunesImageBase, ImageBase, ImageFaviconModel::ImageFaviconBase, ImageItemModel::ImageItemBase, ItemsBase, ItemsBase::ItemBase, ItemsBase::ItemBase::ContentBase, ItemsBase::ItemBase::DescriptionBase, ItemsBase::ItemBase::EnclosureBase, ItemsBase::ItemBase::GuidBase, ItemsBase::ItemBase::RightsBase, ItemsBase::ItemBase::SourceBase, ItemsBase::ItemBase::SourceBase::IconBase, ItemsBase::ItemBase::SourceBase::LogoBase, ItemsBase::ItemBase::SourceBase::RightsBase, ItemsBase::ItemBase::SourceBase::SubtitleBase, ItemsBase::ItemBase::SourceBase::TitleBase, ItemsBase::ItemBase::TitleBase, RSSBase, TaxonomyTopicModel::TaxonomyTopicsBase, TaxonomyTopicModel::TaxonomyTopicsBase::TaxonomyTopicBase, TaxonomyTopicsModel::TaxonomyTopicsBase, TextinputBase, TrackBackModel::TrackBackAboutsBase, TrackBackModel::TrackBackAboutsBase::TrackBackAboutBase, XMLStyleSheets, XMLStyleSheets::XMLStyleSheet

Constant Summary collapse

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

#initialize(maker) ⇒ Base

Returns a new instance of Base.



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

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

Instance Attribute Details

#makerObject (readonly)

Returns the value of attribute maker.



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

def maker
  @maker
end

Class Method Details

.add_need_initialize_variable(variable_name, init_value = nil, &init_block) ⇒ Object



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

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

.add_other_element(variable_name) ⇒ Object



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

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

.def_array_element(name, plural = nil, klass_name = nil) ⇒ Object



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

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

.def_classed_element(name, class_name = nil, attribute_name = nil) ⇒ Object



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

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

.def_classed_element_without_accessor(name, class_name = nil) ⇒ Object



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

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

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



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

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

.def_csv_element(name, type = nil) ⇒ Object



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

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

.def_other_element(name) ⇒ Object



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

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

.def_other_element_without_accessor(name) ⇒ Object



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

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

.inherited(subclass) ⇒ Object



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

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

.inherited_baseObject



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

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

.need_initialize_variablesObject



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

def need_initialize_variables
  inherited_array_reader("NEED_INITIALIZE_VARIABLES")
end

.other_elementsObject



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

def other_elements
  inherited_array_reader("OTHER_ELEMENTS")
end

Instance Method Details

#have_required_values?Boolean

Returns:

  • (Boolean)


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

def have_required_values?
  not_set_required_variables.empty?
end

#variable_is_set?Boolean

Returns:

  • (Boolean)


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

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