Class: Spectifly::Entity

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

Defined Under Namespace

Classes: Invalid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Entity

Returns a new instance of Entity.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spectifly/entity.rb', line 36

def initialize(path, options = {})
  @options = options
  @path = path
  @name = File.basename(@path).sub(/(.*)\.entity$/, '\1')
  @parsed_yaml = YAML.load_file(@path)
  @root = @parsed_yaml.keys.first
   = @parsed_yaml.values.first
  @fields = .delete('Fields')
  @relationships = .delete('Related Entities') || {}
  if @presented_as = options[:presenter]
    @path = @presented_as.path
    @name = @presented_as.name
  end
  unless valid?
    raise Invalid, @errors.join(', ')
  end
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



6
7
8
# File 'lib/spectifly/entity.rb', line 6

def fields
  @fields
end

#metadataObject

Returns the value of attribute metadata.



6
7
8
# File 'lib/spectifly/entity.rb', line 6

def 
  
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/spectifly/entity.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/spectifly/entity.rb', line 5

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/spectifly/entity.rb', line 5

def path
  @path
end

#presented_asObject (readonly)

Returns the value of attribute presented_as.



5
6
7
# File 'lib/spectifly/entity.rb', line 5

def presented_as
  @presented_as
end

#relationshipsObject

Returns the value of attribute relationships.



6
7
8
# File 'lib/spectifly/entity.rb', line 6

def relationships
  @relationships
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/spectifly/entity.rb', line 5

def root
  @root
end

Class Method Details

.from_directory(entities_path, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spectifly/entity.rb', line 13

def from_directory(entities_path, options = {})
  presenter_path = options[:presenter_path]
  entities = {}
  entities_glob = File.join(entities_path, '*.entity')
  Dir[entities_glob].each do |path|
    path = File.expand_path(path)
    entity = Spectifly::Entity.parse(path)
    entities[entity.name] = entity
  end
  if presenter_path
    presenters_glob = File.join(presenter_path, '*.entity')
    Dir[presenters_glob].each do |path|
      path = File.expand_path(path)
      presenter = Spectifly::Entity.parse(path)
      base_entity = entities[Spectifly::Support.tokenize(presenter.root)]
      entity = base_entity.present_as(presenter)
      entities[entity.name] = entity
    end
  end
  entities
end

.parse(*args) ⇒ Object



9
10
11
# File 'lib/spectifly/entity.rb', line 9

def parse(*args)
  new(*args)
end

Instance Method Details

#attributes_for_field_by_base_name(base_name) ⇒ Object



61
62
63
64
65
# File 'lib/spectifly/entity.rb', line 61

def attributes_for_field_by_base_name(base_name)
  @fields.select { |name, attributes|
    name.gsub(/\W+$/, '') == base_name
  }.values.first
end

#attributes_for_relationship_by_base_name(type, base_name) ⇒ Object



67
68
69
70
71
# File 'lib/spectifly/entity.rb', line 67

def attributes_for_relationship_by_base_name(type, base_name)
  @relationships[type].select { |name, attributes|
    name.gsub(/\W+$/, '') == base_name
  }.values.first
end

#present_as(presenter_entity) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/spectifly/entity.rb', line 73

def present_as(presenter_entity)
  unless @root == presenter_entity.root
    raise ArgumentError, "Presenter entity has different root"
  end
  merged_fields = {}
  merged_relationships = {}
  presenter_entity.fields.each_pair do |name, attributes|
    attributes ||= {}
    inherit_from = attributes['Inherits From'] || name.gsub(/\W+$/, '')
    parent_attrs = attributes_for_field_by_base_name(inherit_from)
    merged_fields[name] = (parent_attrs || {}).merge(attributes)
  end

  if presenter_entity.relationships
    presenter_entity.relationships.each_pair do |type, relationships|
      relationships.each do |name, attributes|
        attributes ||= {}
        inherit_from = attributes['Inherits From'] || name.gsub(/\W+$/, '')
        parent_attrs = attributes_for_relationship_by_base_name(type, inherit_from)
        (merged_relationships[type] ||= {})[name] = (parent_attrs || {}).merge(attributes)
      end
    end
  end

  merged_entity = self.class.parse(
    path, options.merge(:presenter => presenter_entity)
  )
  merged_entity.relationships = merged_relationships
  merged_entity.fields = merged_fields
  merged_entity..merge!(presenter_entity.)
  merged_entity
end

#valid?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/spectifly/entity.rb', line 54

def valid?
  @errors = []
  @errors << 'Exactly one root element required' unless @parsed_yaml.count == 1
  @errors << 'Entity is missing "Fields" key' if @fields.nil?
  @errors.empty?
end