Class: Spectifly::Base::EntityNode

Inherits:
Object
  • Object
show all
Defined in:
lib/spectifly/base/entity_node.rb

Direct Known Subclasses

Association, Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, options = {}) ⇒ EntityNode

Returns a new instance of EntityNode.



7
8
9
10
11
12
13
# File 'lib/spectifly/base/entity_node.rb', line 7

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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/spectifly/base/entity_node.rb', line 4

def attributes
  @attributes
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/spectifly/base/entity_node.rb', line 4

def description
  @description
end

#exampleObject

Returns the value of attribute example.



4
5
6
# File 'lib/spectifly/base/entity_node.rb', line 4

def example
  @example
end

#inherits_fromObject

Returns the value of attribute inherits_from.



4
5
6
# File 'lib/spectifly/base/entity_node.rb', line 4

def inherits_from
  @inherits_from
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/spectifly/base/entity_node.rb', line 4

def name
  @name
end

#restrictionsObject

Returns the value of attribute restrictions.



4
5
6
# File 'lib/spectifly/base/entity_node.rb', line 4

def restrictions
  @restrictions
end

#validationsObject

Returns the value of attribute validations.



4
5
6
# File 'lib/spectifly/base/entity_node.rb', line 4

def validations
  @validations
end

Instance Method Details

#display_typeObject



42
43
44
# File 'lib/spectifly/base/entity_node.rb', line 42

def display_type
  type
end

#extract_attributesObject



15
16
17
18
19
20
21
# File 'lib/spectifly/base/entity_node.rb', line 15

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

#extract_restrictionsObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/spectifly/base/entity_node.rb', line 23

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

#required?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/spectifly/base/entity_node.rb', line 50

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

#typeObject



38
39
40
# File 'lib/spectifly/base/entity_node.rb', line 38

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

#unique?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/spectifly/base/entity_node.rb', line 46

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