Class: Trailblazer::Loader
- Inherits:
-
Object
- Object
- Trailblazer::Loader
- Defined in:
- lib/trailblazer/loader.rb,
lib/trailblazer/loader/version.rb
Defined Under Namespace
Classes: Pipeline
Constant Summary collapse
- FindDirectories =
->(input, ) { Dir.glob("#{[:concepts_root]}**/") }
- FindConcepts =
Filter out all directories containing /(callback|cell|contract|operation|policy|representer|view)/
->(input, ) { input.reject { |dir| (dir.split(File::SEPARATOR) & [:concept_dirs]).any? } }
- PrintConcepts =
->(input, ) { 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, ) { input.sort { |a, b| a.split(File::SEPARATOR).size <=> b.split(File::SEPARATOR).size } }
- ConceptName =
Extract concept name from path, e.g. /api/v1/comment.
->(input, ) { [:name] = input.sub([:concepts_root], "").chomp("/"); [] }
- ConceptFiles =
Find all .rb files in one particular concept directory, e.g. as in /concepts/comment/*.rb.
->(input, ) do input + # allow injecting other dependencies, e.g. app/models/comment.rb. Dir.glob("#{[:concepts_root]}#{[:name]}/*.rb") + # .rb files directly in this concept. Dir.glob("#{[:concepts_root]}#{[:name]}/*/*.rb"). # .rb in :concept/operation/*.rb find_all { |file| (file.split(File::SEPARATOR) & [:concept_dirs]).any? } # 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, ) { input.sort { |a, b| a =~ /operation/ && b !~ /operation/ ? 1 : a !~ /operation/ && b =~ /operation/ ? -1 : a <=> b } }
- SortCreateFirst =
->(input, ) { input.sort }
- AddConceptFiles =
->(input, ) { input }
- VERSION =
"0.1.2"
Instance Method Summary collapse
-
#call(options = {}, &block) ⇒ Object
Please note that this is subject to change - we’re still finding out the best way to explicitly load files.
- #concept_dirs ⇒ Object
- #debug(files, options) ⇒ Object
- #default_circuit ⇒ Object
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.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/trailblazer/loader.rb', line 14 def call(={}, &block) [:root] ||= "." [:concepts_root] ||= "#{[:root]}/app/concepts/" [:concept_dirs] ||= concept_dirs pipeline = [:pipeline] || default_circuit if args = [: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 if args = [:prepend] pipeline << args end files = pipeline.([], ).flatten debug(files, ) load_files(files, &block) end |
#concept_dirs ⇒ Object
7 8 9 10 |
# File 'lib/trailblazer/loader.rb', line 7 def concept_dirs %w{ callback cell contract operation policy representer view callbacks cells contracts operations policies representers views } end |
#debug(files, options) ⇒ Object
45 46 47 |
# File 'lib/trailblazer/loader.rb', line 45 def debug(files, ) pp files if [:debug] end |
#default_circuit ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/trailblazer/loader.rb', line 36 def default_circuit Pipeline[ FindDirectories, FindConcepts, SortByLevel, Pipeline::Collect[ConceptName, ConceptFiles, SortCreateFirst, SortOperationLast, AddConceptFiles] # per concept. ] end |