Class: Trailblazer::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/loader.rb,
lib/trailblazer/loader/version.rb

Defined Under Namespace

Classes: Pipeline

Constant Summary collapse

FindDirectories =
->(input, options) { Dir.glob("#{options[:concepts_root]}**/") }
FindConcepts =

Filter out all directories containing /(callback|cell|contract|operation|policy|representer|view)/

->(input, options) { input.shift; input.reject { |dir| dir =~ /(#{options[:concept_dirs].join("|")})/ } }
PrintConcepts =
->(input, options) { puts "  concepts: #{input.inspect}"; input }
SortByLevel =

lame heuristic, but works for me: sort by directory levels. app/concepts/comment app/concepts/api/v1/comment

->(input, options) { input.sort { |a, b| a.split("/").size <=> b.split("/").size } }
ConceptName =

Extract concept name from path, e.g. /api/v1/comment.

->(input, options) { options[:name] = input.sub(options[:concepts_root], "").chomp("/"); [] }
ConceptFiles =

Find all .rb files in one particular concept directory, e.g. as in /concepts/comment/*.rb.

->(input, options) do
  input + # allow injecting other dependencies, e.g. app/models/comment.rb.

  Dir.glob("#{options[:concepts_root]}#{options[:name]}/*.rb") +        # .rb files directly in this concept.
    Dir.glob("#{options[:concepts_root]}#{options[:name]}/*/*.rb").     # .rb in :concept/operation/*.rb
    find_all { |file| file =~ /(#{options[:concept_dirs].join("|")})/ } # but only those, no sub-concepts!
end
SortOperationLast =

operation files should be loaded after callbacks, policies, and the like: [callback.rb, contract.rb, policy.rb, operation.rb]

->(input, options) { input.sort { |a, b| a =~ /operation/ && b !~ /operation/ ? 1 : a !~ /operation/ && b =~ /operation/ ? 0 : a <=> b  } }
SortCreateFirst =
->(input, options) { input.sort }
AddConceptFiles =
->(input, options) { input }
VERSION =
"0.0.9"

Instance Method Summary collapse

Instance Method Details

#call(options = {}, &block) ⇒ Object

Please note that this is subject to change - we’re still finding out the best way to explicitly load files.

NOTE: i will most probably use call_sheet and dry-container here soon.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/trailblazer/loader.rb', line 10

def call(options={}, &block)
  options[:concepts_root] ||= "app/concepts/"
  options[:concept_dirs] = concept_dirs

  pipeline = options[:pipeline] || Pipeline[
    FindDirectories,
    FindConcepts,
    SortByLevel,
    Pipeline::Collect[ConceptName, ConceptFiles, SortCreateFirst, SortOperationLast, AddConceptFiles] # per concept.
  ]

  if args = options[:insert] # FIXME: this only implements a sub-set.
    # pipeline = Representable::Pipeline::Insert.(pipeline, *args) # FIXME: implement :before in Pipeline.
    pipeline.last.insert(pipeline.last.index(args.last[:before]), args.first)
  end

  files =  pipeline.([], options).flatten

  debug(files, options)

  load_files(files, &block)
end

#concept_dirsObject



37
38
39
# File 'lib/trailblazer/loader.rb', line 37

def concept_dirs
  %w{ callback cell contract operation policy representer view }
end

#debug(files, options) ⇒ Object



33
34
35
# File 'lib/trailblazer/loader.rb', line 33

def debug(files, options)
  pp files if options[:debug]
end