Sha256: 8d4ddf3432653920c13a1411652c7a8e3b586cb64abc2809860289f539de5b3f

Contents?: true

Size: 719 Bytes

Versions: 1

Compression:

Stored size: 719 Bytes

Contents

module Tessitura
  module AttrAccessorOfType
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def attr_accessor_of_type(name, options = {})
        xpath = options.fetch :xpath, "//#{name}"
        type = options.fetch :type, String

        define_method name do
          return unless el = doc.at_xpath(xpath)
          case type.to_s
          when 'Integer' then el.text.to_i
          when 'Float' then el.text.to_f
          when 'DateTime' then DateTime.parse(el.text)
          when 'TrueClass' then el.text == 'Y'
          else el.text
          end
        end

        alias_method("#{name}?".to_sym, name) if type == TrueClass
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tessitura-0.1.0 lib/tessitura/attr_accessor_of_type.rb