Class: Blueprint::StructureDesignContext

Inherits:
DesignContext show all
Defined in:
lib/blueprint/api/rails.rb

Overview

Design context for a structure (a group of nodes and links)

Instance Method Summary collapse

Methods inherited from DesignContext

#send

Constructor Details

#initialize(api_key, structure_id) ⇒ StructureDesignContext

Returns a new instance of StructureDesignContext.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/blueprint/api/rails.rb', line 79

def initialize(api_key, structure_id)
  @api_key = api_key
  @structure_id = structure_id
  @branch = `git rev-parse --abbrev-ref HEAD 2>&1`.strip! || 'master'

  # initialise faraday
  @conn = Faraday.new(:url => BLUEPRINT_SERVER) do |faraday|
    # faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Instance Method Details

#begin(name) ⇒ Object



139
140
141
# File 'lib/blueprint/api/rails.rb', line 139

def begin(name)
  ActivityDesignContext.new(@api_key, @structure_id, name)
end

#classifier(from, conditions, to) ⇒ Object

creates a new message classifier



144
145
146
147
148
149
150
151
152
# File 'lib/blueprint/api/rails.rb', line 144

def classifier(from, conditions, to)
  self.send CLASSIFIER_NEW,
            {
                :from => from,
                :conditions => conditions,
                :to => to
            }
  nil
end

#concept(name) ⇒ Object



130
131
132
# File 'lib/blueprint/api/rails.rb', line 130

def concept(name)
  ConceptDesignContext.new(@api_key, @structure_id, name)
end

#element(name) ⇒ Object



126
127
128
# File 'lib/blueprint/api/rails.rb', line 126

def element(name)
  StructuralElementDesignContext.new(@api_key, @structure_id, name)
end

creates a design context between source and target



135
136
137
# File 'lib/blueprint/api/rails.rb', line 135

def link(source, target)
  LinkDesignContext.new(@api_key, @structure_id, source, target)
end

#log(message = { }, extras = { }, type = nil, source = nil, target = nil) ⇒ Object

logs a message between two nodes in the structure



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/blueprint/api/rails.rb', line 155

def log(message = { }, extras = { }, type = nil, source = nil, target = nil)
  properties = Hash.new.tap do |h|
    h[:source] = source unless source.blank?
    h[:target] = target unless target.blank?
  end

  payload = Hash.new.tap do |h|
    h[:type] = type unless type.blank?
    # h[:user] = current_user.id unless current_user.nil? || current_user.id.nil?
    h[:payload] = {
        :message => message,
        :extras => extras
    }
  end

  # send the message
  self.send MESSAGE, properties, payload

  # return nil so that no further calls can be made to the fluent API
  nil
end

#scanObject

performs an extract (that is, scans the code base for architectural elements) TODO this scan can be arbitrarily complex - we could scan for anything TODO add support of passing in custom scanners



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/blueprint/api/rails.rb', line 94

def scan

  p 'Scanning for controllers...'

  # scan for all controllers (any file with the name *Controller)
  controllers = Dir[Rails.root.join('app/controllers/*_controller.rb')].map { |path|
    path.match(/(\w+_controller).rb/); $1.gsub!(/_/, ' ').titleize
  }.reject { |clazz|
    clazz.eql?('Application Controller')   # ignore the ApplicationController superclass
  }.compact

  p "Found #{controllers.length} controllers - sending to Blueprint"

  # send the controllers to Blueprint
  controllers.each { |c|
    self.element(c)       # register the element
  }

  # now scan for models
  models = Dir[Rails.root.join('app/models/*.rb')].map { |path|
    path.match(/(\w+).rb/); $1.titleize
  }.compact

  p "Found #{models.length} models - sending to Blueprint"

  models.each { |m|
    self.concept(m)           # register the concept
  }

  p 'Scan complete'
end