Class: Librarian::Dsl::Target
- Inherits:
-
Object
- Object
- Librarian::Dsl::Target
- Defined in:
- lib/librarian/dsl/target.rb
Defined Under Namespace
Classes: SourceShortcutDefinitionReceiver
Constant Summary collapse
- SCOPABLES =
[:source, :sources]
Instance Attribute Summary collapse
-
#dependency_name ⇒ Object
readonly
Returns the value of attribute dependency_name.
-
#dependency_type ⇒ Object
readonly
Returns the value of attribute dependency_type.
-
#dsl ⇒ Object
readonly
Returns the value of attribute dsl.
-
#source_shortcuts ⇒ Object
readonly
Returns the value of attribute source_shortcuts.
-
#source_type_names ⇒ Object
readonly
Returns the value of attribute source_type_names.
-
#source_types ⇒ Object
readonly
Returns the value of attribute source_types.
-
#source_types_map ⇒ Object
readonly
Returns the value of attribute source_types_map.
-
#source_types_reverse_map ⇒ Object
readonly
Returns the value of attribute source_types_reverse_map.
Instance Method Summary collapse
- #define_source_shortcut(name, definition) ⇒ Object
- #dependency(name, *args) ⇒ Object
- #environment ⇒ Object
- #exclusion(name) ⇒ Object
- #extract_source_parts(options) ⇒ Object
-
#initialize(dsl) ⇒ Target
constructor
A new instance of Target.
- #normalize_source_options(name, param, options) ⇒ Object
- #precache_sources(sources) ⇒ Object
- #scope ⇒ Object
- #scope_or_directive(scoped_block = nil) ⇒ Object
- #source(name, param = nil, options = nil, &block) ⇒ Object
- #source_from_options(options) ⇒ Object
- #source_from_params(name, param, options) ⇒ Object
- #source_from_source_shortcut_definition(definition) ⇒ Object
- #to_spec ⇒ Object
Constructor Details
#initialize(dsl) ⇒ Target
Returns a new instance of Target.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/librarian/dsl/target.rb', line 34 def initialize(dsl) self.dsl = dsl @dependency_name = dsl.dependency_name @dependency_type = dsl.dependency_type @source_types = dsl.source_types @source_types_map = Hash[source_types] @source_types_reverse_map = Hash[source_types.map{|pair| a, b = pair ; [b, a]}] @source_type_names = source_types.map{|t| t[0]} @source_cache = {} @source_shortcuts = {} @dependencies = [] @exclusions = [] SCOPABLES.each do |scopable| instance_variable_set(:"@#{scopable}", []) end dsl.source_shortcuts.each do |name, param| define_source_shortcut(name, param) end end |
Instance Attribute Details
#dependency_name ⇒ Object (readonly)
Returns the value of attribute dependency_name.
30 31 32 |
# File 'lib/librarian/dsl/target.rb', line 30 def dependency_name @dependency_name end |
#dependency_type ⇒ Object (readonly)
Returns the value of attribute dependency_type.
30 31 32 |
# File 'lib/librarian/dsl/target.rb', line 30 def dependency_type @dependency_type end |
#dsl ⇒ Object
Returns the value of attribute dsl.
27 28 29 |
# File 'lib/librarian/dsl/target.rb', line 27 def dsl @dsl end |
#source_shortcuts ⇒ Object (readonly)
Returns the value of attribute source_shortcuts.
31 32 33 |
# File 'lib/librarian/dsl/target.rb', line 31 def source_shortcuts @source_shortcuts end |
#source_type_names ⇒ Object (readonly)
Returns the value of attribute source_type_names.
31 32 33 |
# File 'lib/librarian/dsl/target.rb', line 31 def source_type_names @source_type_names end |
#source_types ⇒ Object (readonly)
Returns the value of attribute source_types.
31 32 33 |
# File 'lib/librarian/dsl/target.rb', line 31 def source_types @source_types end |
#source_types_map ⇒ Object (readonly)
Returns the value of attribute source_types_map.
31 32 33 |
# File 'lib/librarian/dsl/target.rb', line 31 def source_types_map @source_types_map end |
#source_types_reverse_map ⇒ Object (readonly)
Returns the value of attribute source_types_reverse_map.
31 32 33 |
# File 'lib/librarian/dsl/target.rb', line 31 def source_types_reverse_map @source_types_reverse_map end |
Instance Method Details
#define_source_shortcut(name, definition) ⇒ Object
165 166 167 168 |
# File 'lib/librarian/dsl/target.rb', line 165 def define_source_shortcut(name, definition) source = source_from_source_shortcut_definition(definition) source_shortcuts[name] = source end |
#dependency(name, *args) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/librarian/dsl/target.rb', line 58 def dependency(name, *args) = args.last.is_a?(Hash) ? args.pop : {} source = () || @source dep = dependency_type.new(name, args, source) @dependencies << dep end |
#environment ⇒ Object
170 171 172 |
# File 'lib/librarian/dsl/target.rb', line 170 def environment dsl.environment end |
#exclusion(name) ⇒ Object
65 66 67 |
# File 'lib/librarian/dsl/target.rb', line 65 def exclusion(name) @exclusions << name end |
#extract_source_parts(options) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/librarian/dsl/target.rb', line 126 def extract_source_parts() if name = source_type_names.find{|name| .key?(name)} = .dup param = .delete(name) [name, param, ] else nil end end |
#normalize_source_options(name, param, options) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/librarian/dsl/target.rb', line 118 def (name, param, ) if name.is_a?(Hash) extract_source_parts(name) else [name, param, ] end end |
#precache_sources(sources) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/librarian/dsl/target.rb', line 88 def precache_sources(sources) sources.each do |source| key = [source_types_reverse_map[source.class], *source.to_spec_args] source_cache[key] = source end end |
#scope ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/librarian/dsl/target.rb', line 95 def scope currents = { } SCOPABLES.each do |scopable| currents[scopable] = instance_variable_get(:"@#{scopable}").dup end yield ensure SCOPABLES.reverse.each do |scopable| instance_variable_set(:"@#{scopable}", currents[scopable]) end end |
#scope_or_directive(scoped_block = nil) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/librarian/dsl/target.rb', line 107 def scope_or_directive(scoped_block = nil) unless scoped_block yield else scope do yield scoped_block.call end end end |
#source(name, param = nil, options = nil, &block) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/librarian/dsl/target.rb', line 69 def source(name, param = nil, = nil, &block) if !(Hash === name) && [Array, Hash, Proc].any?{|c| c === param} && ! && !block define_source_shortcut(name, param) elsif !(Hash === name) && !param && ! source = source_shortcuts[name] scope_or_directive(block) do @source = source @sources = @sources.dup << source end else name, param, = *(name, param, || {}) source = source_from_params(name, param, ) scope_or_directive(block) do @source = source @sources = @sources.dup << source end end end |
#source_from_options(options) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/librarian/dsl/target.rb', line 136 def () if [:source] source_shortcuts[[:source]] elsif source_parts = extract_source_parts() source_from_params(*source_parts) else nil end end |
#source_from_params(name, param, options) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/librarian/dsl/target.rb', line 146 def source_from_params(name, param, ) source_cache[[name, param, ]] ||= begin type = source_types_map[name] type.from_spec_args(environment, param, ) end end |
#source_from_source_shortcut_definition(definition) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/librarian/dsl/target.rb', line 153 def source_from_source_shortcut_definition(definition) case definition when Array source_from_params(*definition) when Hash (definition) when Proc receiver = SourceShortcutDefinitionReceiver.new(self) receiver.instance_eval(&definition) end end |