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
- #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 |
# 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 = [] 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
160 161 162 163 |
# File 'lib/librarian/dsl/target.rb', line 160 def define_source_shortcut(name, definition) source = source_from_source_shortcut_definition(definition) source_shortcuts[name] = source end |
#dependency(name, *args) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/librarian/dsl/target.rb', line 57 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
165 166 167 |
# File 'lib/librarian/dsl/target.rb', line 165 def environment dsl.environment end |
#extract_source_parts(options) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/librarian/dsl/target.rb', line 121 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
113 114 115 116 117 118 119 |
# File 'lib/librarian/dsl/target.rb', line 113 def (name, param, ) if name.is_a?(Hash) extract_source_parts(name) else [name, param, ] end end |
#precache_sources(sources) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/librarian/dsl/target.rb', line 83 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
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/librarian/dsl/target.rb', line 90 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
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/librarian/dsl/target.rb', line 102 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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/librarian/dsl/target.rb', line 64 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
131 132 133 134 135 136 137 138 139 |
# File 'lib/librarian/dsl/target.rb', line 131 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
141 142 143 144 145 146 |
# File 'lib/librarian/dsl/target.rb', line 141 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
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/librarian/dsl/target.rb', line 148 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 |