aki017/ZeroFormatter.rb

View on GitHub
lib/zero_formatter/zero_formattable.rb

Summary

Maintainability
A
0 mins
Test Coverage
module ZeroFormatter
  module ZeroFormattable
    def self.included(klass)
      klass.extend ClassMethods
    end

    module ClassMethods
      def fields
        @fields || []
      end

      def field(name, type, options={})
        @fields ||= []
        index = options[:index] || @fields.size == 0 ? 0 : @fields.last[:index] + 1
        @fields << {
          index: index,
          name: name,
          type: type,
          options: options,
        }

        attr_accessor name
        return
      end
    end
  end
end