Sha256: 1141fd34513a1f9c181354d225a6de2100d6d3ee6d5a9ff05717f3cea78d0f1f

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

module Yema
  module Virtus
    class Attribute

      attr_reader :attribute
      DEFAULT_BUILDERS  = [ Builder::StrongType, Builder::Required ].freeze

      def initialize(attribute)
        @attribute = attribute
      end

      def self.specific_builders
        [].freeze
      end

      def self.builders
        DEFAULT_BUILDERS + specific_builders
      end

      def rules
        rules = []
        self.class.builders.each do |builder|
          rule = builder.new(attribute).build
          rules << rule if rule
        end
        rules.to_set
      end

      def self.build(attribute)
        klass = determine_type(attribute)
        klass.new(attribute)
      end

      private

      # TODO: find a better way for mapping virtus type
      def self.determine_type(attribute)
        if    attribute.kind_of?(::Virtus::Attribute::String);  self::String
        elsif attribute.kind_of?(::Virtus::Attribute::Numeric); self::Numeric
        elsif attribute.kind_of?(::Virtus::Attribute::Array);   self::Array
        elsif attribute.kind_of?(::Virtus::Attribute::Boolean); self
        else
          raise TypeError, "#{attribute.class} is not supported"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
yema-0.0.3 lib/yema/virtus/attribute.rb
yema-0.0.2 lib/yema/virtus/attribute.rb
yema-0.0.1 lib/yema/virtus/attribute.rb