Module: RSS::BaseModel
Instance Method Summary
- #install_date_element(tag_name, uri, occurs, name = nil, type = nil, disp_name = nil)
-
#install_have_attribute_element(tag_name, uri, occurs, name = nil, type = nil)
Alias for #install_have_child_element.
- #install_have_child_element(tag_name, uri, occurs, name = nil, type = nil) (also: #install_have_attribute_element)
- #install_have_children_element(tag_name, uri, occurs, name = nil, plural_name = nil)
- #install_text_element(tag_name, uri, occurs, name = nil, type = nil, disp_name = nil)
- #boolean_writer(name, disp_name = name) private
- #content_writer(name, disp_name = name) private
- #convert_attr_reader(*attrs) private
- #csv_attr_reader(*attrs) private
- #csv_integer_writer(name, disp_name = name) private
- #csv_writer(name, disp_name = name) private
- #date_writer(name, type, disp_name = name) private
- #def_children_accessor(accessor_name, plural_name) private
- #explicit_clean_other_attr_reader(*attrs) private
- #explicit_clean_other_writer(name, disp_name = name) private
- #inherit_convert_attr_reader(*attrs) private
- #install_element(name, postfix = "") private
- #integer_writer(name, disp_name = name) private
- #positive_integer_writer(name, disp_name = name) private
- #text_type_writer(name, disp_name = name) private
- #uri_convert_attr_reader(*attrs) private
- #yes_other_attr_reader(*attrs) private
- #yes_other_writer(name, disp_name = name) private
Utils
- Included
#element_initialize_arguments? | This method is used inside of several different objects to determine if special behavior is needed in the constructor. |
#get_file_and_line_from_caller | Returns an array of two elements: the filename where the calling method is located, and the line number where it is defined. |
#h, | |
#html_escape | Takes a string |
#new_with_value_if_need | If |
#to_class_name | Given a |
Instance Method Details
#boolean_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 501
def boolean_writer(name, disp_name=name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{name}=(new_value) if new_value.nil? @#{name} = new_value else if @do_validate and ![true, false, "true", "false"].include?(new_value) raise NotAvailableValueError.new('#{disp_name}', new_value) end if [true, false].include?(new_value) @#{name} = new_value else @#{name} = new_value == "true" end end end EOC end
#content_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 533
def content_writer(name, disp_name=name) klass_name = "self.class::#{Utils.to_class_name(name)}" module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{name}=(new_value) if new_value.is_a?(#{klass_name}) @#{name} = new_value else @#{name} = #{klass_name}.new @#{name}.content = new_value end end EOC end
#convert_attr_reader(*attrs) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 367
def convert_attr_reader(*attrs) attrs.each do |attr| module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{attr} convert(@#{attr}) end EOC end end
#csv_attr_reader(*attrs) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 399
def csv_attr_reader(*attrs) separator = nil if attrs.last.is_a?(Hash) = attrs.pop separator = [:separator] end separator ||= ", " attrs.each do |attr| module_eval(<<-EOC, __FILE__, __LINE__ + 1) attr_reader(:#{attr}) def #{attr}_content if @#{attr}.nil? @#{attr} else @#{attr}.join(#{separator.dump}) end end EOC end end
#csv_integer_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 575
def csv_integer_writer(name, disp_name=name) module_eval(<<-EOC, __FILE__, __LINE__ + 1) def #{name}=(new_value) @#{name} = Utils::CSV.parse(new_value) {|v| Integer(v)} end EOC end
#csv_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 567
def csv_writer(name, disp_name=name) module_eval(<<-EOC, __FILE__, __LINE__ + 1) def #{name}=(new_value) @#{name} = Utils::CSV.parse(new_value) end EOC end
#date_writer(name, type, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 420
def date_writer(name, type, disp_name=name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{name}=(new_value) if new_value.nil? @#{name} = new_value elsif new_value.kind_of?(Time) @#{name} = new_value.dup else if @do_validate begin @#{name} = Time.__send__('#{type}', new_value) rescue ArgumentError raise NotAvailableValueError.new('#{disp_name}', new_value) end else @#{name} = nil if /\\A\\s*\\z/ !~ new_value.to_s begin unless Date._parse(new_value, false).empty? @#{name} = Time.parse(new_value) end rescue ArgumentError end end end end # Is it need? if @#{name} class << @#{name} undef_method(:to_s) alias_method(:to_s, :#{type}) end end end EOC end
#def_children_accessor(accessor_name, plural_name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 583
def def_children_accessor(accessor_name, plural_name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{plural_name} @#{accessor_name} end def #{accessor_name}(*args) if args.empty? @#{accessor_name}.first else @#{accessor_name}[*args] end end def #{accessor_name}=(*args) receiver = self.class.name warn("Don't use `\#{receiver}\##{accessor_name} = XXX'/" \ "`\#{receiver}\#set_#{accessor_name}(XXX)'. " \ "Those APIs are not sense of Ruby. " \ "Use `\#{receiver}\##{plural_name} << XXX' instead of them.", uplevel: 1) if args.size == 1 @#{accessor_name}.push(args[0]) else @#{accessor_name}.__send__("[]=", *args) end end alias_method(:set_#{accessor_name}, :#{accessor_name}=) EOC end
#explicit_clean_other_attr_reader(*attrs) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 377
def explicit_clean_other_attr_reader(*attrs) attrs.each do |attr| module_eval(<<-EOC, __FILE__, __LINE__ + 1) attr_reader(:#{attr}) def #{attr}? ExplicitCleanOther.parse(@#{attr}) end EOC end end
#explicit_clean_other_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 547
def explicit_clean_other_writer(name, disp_name=name) module_eval(<<-EOC, __FILE__, __LINE__ + 1) def #{name}=(value) value = (value ? "yes" : "no") if [true, false].include?(value) @#{name} = value end EOC end
#inherit_convert_attr_reader(*attrs) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 327
def inherit_convert_attr_reader(*attrs) attrs.each do |attr| module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{attr}_without_inherit convert(@#{attr}) end def #{attr} if @#{attr} #{attr}_without_inherit elsif @parent @parent.#{attr} else nil end end EOC end end
#install_date_element(tag_name, uri, occurs, name = nil, type = nil, disp_name = nil)
[ GitHub ]# File 'lib/rss/rss.rb', line 282
def install_date_element(tag_name, uri, occurs, name=nil, type=nil, disp_name=nil) name ||= tag_name type ||= :w3cdtf disp_name ||= name self::ELEMENTS << name add_need_initialize_variable(name) install_model(tag_name, uri, occurs, name) # accessor convert_attr_reader name date_writer(name, type, disp_name) install_element(name) do |n, elem_name| <<-EOC if @#{n} rv = "\#{indent}<#{elem_name}>" value = html_escape(@#{n}.#{type}) if need_convert rv << convert(value) else rv << value end rv << "</#{elem_name}>" rv else '' end EOC end end
#install_element(name, postfix = "") (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 315
def install_element(name, postfix="") elem_name = name.sub('_', ':') method_name = "#{name}_element#{postfix}" add_to_element_method(method_name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{method_name}(need_convert=true, indent='') #{yield(name, elem_name)} end private :#{method_name} EOC end
#install_have_attribute_element(tag_name, uri, occurs, name = nil, type = nil)
Alias for #install_have_child_element.
# File 'lib/rss/rss.rb', line 226
alias_method(:install_have_attribute_element, :install_have_child_element)
#install_have_child_element(tag_name, uri, occurs, name = nil, type = nil) Also known as: #install_have_attribute_element
[ GitHub ]# File 'lib/rss/rss.rb', line 208
def install_have_child_element(tag_name, uri, occurs, name=nil, type=nil) name ||= tag_name add_need_initialize_variable(name) install_model(tag_name, uri, occurs, name) writer_type, reader_type = type def_corresponded_attr_writer name, writer_type def_corresponded_attr_reader name, reader_type install_element(name) do |n, elem_name| <<-EOC if @#{n} "\#{@#{n}.to_s(need_convert, indent)}" else '' end EOC end end
#install_have_children_element(tag_name, uri, occurs, name = nil, plural_name = nil)
[ GitHub ]# File 'lib/rss/rss.rb', line 228
def install_have_children_element(tag_name, uri, occurs, name=nil, plural_name=nil) name ||= tag_name plural_name ||= "#{name}s" add_have_children_element(name, plural_name) add_plural_form(name, plural_name) install_model(tag_name, uri, occurs, plural_name, true) def_children_accessor(name, plural_name) install_element(name, "s") do |n, elem_name| <<-EOC rv = [] @#{n}.each do |x| value = "\#{x.to_s(need_convert, indent)}" rv << value if /\\A\\s*\\z/ !~ value end rv.join("\n") EOC end end
#install_text_element(tag_name, uri, occurs, name = nil, type = nil, disp_name = nil)
[ GitHub ]# File 'lib/rss/rss.rb', line 248
def install_text_element(tag_name, uri, occurs, name=nil, type=nil, disp_name=nil) name ||= tag_name disp_name ||= name self::ELEMENTS << name unless self::ELEMENTS.include?(name) add_need_initialize_variable(name) install_model(tag_name, uri, occurs, name) def_corresponded_attr_writer(name, type, disp_name) def_corresponded_attr_reader(name, type || :convert) install_element(name) do |n, elem_name| <<-EOC if respond_to?(:#{n}_content) content = #{n}_content else content = @#{n} end if content rv = "\#{indent}<#{elem_name}>" value = html_escape(content) if need_convert rv << convert(value) else rv << value end rv << "</#{elem_name}>" rv else '' end EOC end end
#integer_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 459
def integer_writer(name, disp_name=name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{name}=(new_value) if new_value.nil? @#{name} = new_value else if @do_validate begin @#{name} = Integer(new_value) rescue ArgumentError raise NotAvailableValueError.new('#{disp_name}', new_value) end else @#{name} = new_value.to_i end end end EOC end
#positive_integer_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 479
def positive_integer_writer(name, disp_name=name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{name}=(new_value) if new_value.nil? @#{name} = new_value else if @do_validate begin tmp = Integer(new_value) raise ArgumentError if tmp <= 0 @#{name} = tmp rescue ArgumentError raise NotAvailableValueError.new('#{disp_name}', new_value) end else @#{name} = new_value.to_i end end end EOC end
#text_type_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 521
def text_type_writer(name, disp_name=name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{name}=(new_value) if @do_validate and !["text", "html", "xhtml", nil].include?(new_value) raise NotAvailableValueError.new('#{disp_name}', new_value) end @#{name} = new_value end EOC end
#uri_convert_attr_reader(*attrs) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 347
def uri_convert_attr_reader(*attrs) attrs.each do |attr| module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{attr}_without_base convert(@#{attr}) end def #{attr} value = #{attr}_without_base return nil if value.nil? if /\\A[a-z][a-z0-9+.\\-]*:/i =~ value value else "\#{base}\#{value}" end end EOC end end
#yes_other_attr_reader(*attrs) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 388
def yes_other_attr_reader(*attrs) attrs.each do |attr| module_eval(<<-EOC, __FILE__, __LINE__ + 1) attr_reader(:#{attr}) def #{attr}? Utils::YesOther.parse(@#{attr}) end EOC end end
#yes_other_writer(name, disp_name = name) (private)
[ GitHub ]# File 'lib/rss/rss.rb', line 556
def yes_other_writer(name, disp_name=name) module_eval(<<-EOC, __FILE__, __LINE__ + 1) def #{name}=(new_value) if [true, false].include?(new_value) new_value = new_value ? "yes" : "no" end @#{name} = new_value end EOC end