Sha256: e3bfaa8839001e98a98c76eec0079bdd7521f2a5f3e460d8ca3e47d348a1acdc

Contents?: true

Size: 1.45 KB

Versions: 13

Compression:

Stored size: 1.45 KB

Contents

module EfoNelfo

  class PostType
    include EfoNelfo::Properties
    include EfoNelfo::HasMany

    class << self

      def for(type, version)
        klass = "EfoNelfo::V#{version_to_namespace(version)}::#{type}"
        const_get(klass) rescue nil
      end

      # Converts version to module version name
      # Example: version_to_namespace("4.2")   # => "42"
      def version_to_namespace(version)
        version.gsub('.', '')
      end

      # Extracts version number from class namespace.
      # Example: EfoNelfo::V41::Some::Class.version  # => "4.1"
      def version
        (version_from_class.to_f / 10).to_s
      end

      def version_from_class
        self.to_s.match(/::V(?<version>\d+)::/)[:version]
      end

      def post_type
        name.split(/::/).last
      end

      def from(hash)
        self.for(hash[:post_type], hash[:version]).new(hash)
      end

      def parse(csv)
        new EfoNelfo::Reader::CSV.new(data: csv).first
      end

    end

    def initialize(*args)
      if args && args.first.is_a?(Array)
        initialize_attributes Hash[properties.keys.zip(args.first)]
      else
        initialize_attributes(*args)
      end
    end

    def post_type
      self.class.post_type
    end

    def version
      self.class.version
    end

    def to_csv
      CSV.generate EfoNelfo::Reader::CSV::CSV_OPTIONS do |csv|
        to_a.each do |row|
          csv << row unless row.empty?
        end
      end
    end

  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
efo_nelfo-2.0.0 lib/efo_nelfo/post_type.rb
efo_nelfo-1.7.0 lib/efo_nelfo/post_type.rb
efo_nelfo-1.6.2 lib/efo_nelfo/post_type.rb
efo_nelfo-1.6.1 lib/efo_nelfo/post_type.rb
efo_nelfo-1.6.0 lib/efo_nelfo/post_type.rb
efo_nelfo-1.5.5 lib/efo_nelfo/post_type.rb
efo_nelfo-1.5.4 lib/efo_nelfo/post_type.rb
efo_nelfo-1.5.3 lib/efo_nelfo/post_type.rb
efo_nelfo-1.5.2 lib/efo_nelfo/post_type.rb
efo_nelfo-1.5.1 lib/efo_nelfo/post_type.rb
efo_nelfo-1.5.0 lib/efo_nelfo/post_type.rb
efo_nelfo-1.4.0 lib/efo_nelfo/post_type.rb
efo_nelfo-1.3.2 lib/efo_nelfo/post_type.rb