Sha256: d7104a5ab690f870d23b3eb8a88643b3f95c6a204ae8aa5098240f17318731a2

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 KB

Contents

module Spectifly
  module Base
    class EntityNode
      attr_accessor :name, :attributes, :description, :example, :validations,
        :restrictions, :inherits_from

      def initialize(field_name, options = {})
        @field_name = field_name
        @tokens = @field_name.match(/(\W+)$/).to_s.scan(/./)
        @attributes = options
        extract_attributes
        extract_restrictions
      end

      def extract_attributes
        @description = @attributes.delete('Description')
        @example = @attributes.delete('Example')
        @type = @attributes.delete('Type')
        @inherits_from = @attributes.delete('Inherits From')
        @validations = [@attributes.delete('Validations')].compact.flatten
      end

      def extract_restrictions
        @restrictions = {}
        unique_validation = @validations.reject! { |v| v =~ /must be unique/i }
        unique_attribute = @attributes.delete("Unique")
        if (unique_validation && unique_attribute.nil?) ^ (unique_attribute.to_s == "true")
          @restrictions['unique'] = true
        elsif unique_validation && !["true", ""].include?(unique_attribute.to_s)
          raise "Field/association #{name} has contradictory information about uniqueness."
        end
      end

      def name
        Spectifly::Support.tokenize(@field_name).gsub(/\W/, '')
      end

      def type
        Spectifly::Support.tokenize(@type)
      end

      def display_type
        type
      end

      def unique?
        @restrictions['unique'] == true
      end

      def required?
        @tokens.include? '*'
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
spectifly-0.0.11 lib/spectifly/base/entity_node.rb
spectifly-0.0.10 lib/spectifly/base/entity_node.rb
spectifly-0.0.9 lib/spectifly/base/entity_node.rb
spectifly-0.0.8 lib/spectifly/base/entity_node.rb
spectifly-0.0.7 lib/spectifly/base/entity_node.rb
spectifly-0.0.6 lib/spectifly/base/entity_node.rb
spectifly-0.0.5 lib/spectifly/base/entity_node.rb
spectifly-0.0.4 lib/spectifly/base/entity_node.rb
spectifly-0.0.3 lib/spectifly/base/entity_node.rb
spectifly-0.0.2 lib/spectifly/base/entity_node.rb
spectifly-0.0.1 lib/spectifly/base/entity_node.rb