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

#check_rules, #determine_remote_repository, #send

Constructor Details

#initialize(api_key, structure_id) ⇒ StructureDesignContext

Returns a new instance of StructureDesignContext.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/blueprint/api/rails.rb', line 152

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



227
228
229
# File 'lib/blueprint/api/rails.rb', line 227

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

#classifier(from, conditions, to) ⇒ Object

creates a new message classifier



232
233
234
235
236
237
238
239
240
# File 'lib/blueprint/api/rails.rb', line 232

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

#concept(name) ⇒ Object

creates a design context for a concept



218
219
220
# File 'lib/blueprint/api/rails.rb', line 218

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

#element(name, code_url = nil) ⇒ Object

creates a design context for a structural element



213
214
215
# File 'lib/blueprint/api/rails.rb', line 213

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

creates a design context between source and target



223
224
225
# File 'lib/blueprint/api/rails.rb', line 223

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

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

logs a message between two nodes in the structure



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/blueprint/api/rails.rb', line 243

def log(source = nil, target = nil, type = nil, message = { }, extras = { })
  # always check the logged message for architectural rules violations
  check_rules(source, target, type)

  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

#scan(repository_type) ⇒ Object

performs an extract (that is, scans the code base for architectural elements)



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/blueprint/api/rails.rb', line 165

def scan(repository_type)
  repo_url = nil

  if repository_type == :github
    p 'GitHub has been specified as the remote repository store'
    repo_url = determine_remote_repository '.'
  end

  p 'Scanning for controllers...'

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

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

  if repository_type == :github
    controllers.each { |c|
      pretty_name = c.gsub(/_/, ' ').titleize
      self.element(pretty_name, "#{repo_url}/blob/master/app/controllers/#{c}.rb")       # register the element
    }

  else
    controllers.each { |c|
      pretty_name = c.gsub(/_/, ' ').titleize
      self.element(pretty_name)       # register the element
    }

  end

  # 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