Class: Librarian::Dsl::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/librarian/dsl/target.rb

Defined Under Namespace

Classes: SourceShortcutDefinitionReceiver

Constant Summary collapse

SCOPABLES =
[:source, :sources]

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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_typeObject (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

#dslObject

Returns the value of attribute dsl.



27
28
29
# File 'lib/librarian/dsl/target.rb', line 27

def dsl
  @dsl
end

#source_shortcutsObject (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_namesObject (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_typesObject (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_mapObject (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_mapObject (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)
  options = args.last.is_a?(Hash) ? args.pop : {}
  source = source_from_options(options) || @source
  dep = dependency_type.new(name, args, source)
  @dependencies << dep
end

#environmentObject



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(options)
  if name = source_type_names.find{|name| options.key?(name)}
    options = options.dup
    param = options.delete(name)
    [name, param, options]
  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 normalize_source_options(name, param, options)
  if name.is_a?(Hash)
    extract_source_parts(name)
  else
    [name, param, options]
  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

#scopeObject



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, options = nil, &block)
  if !(Hash === name) && [Array, Hash, Proc].any?{|c| c === param} && !options && !block
    define_source_shortcut(name, param)
  elsif !(Hash === name) && !param && !options
    source = source_shortcuts[name]
    scope_or_directive(block) do
      @source = source
      @sources = @sources.dup << source
    end
  else
    name, param, options = *normalize_source_options(name, param, options || {})
    source = source_from_params(name, param, options)
    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 source_from_options(options)
  if options[:source]
    source_shortcuts[options[:source]]
  elsif source_parts = extract_source_parts(options)
    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, options)
  source_cache[[name, param, options]] ||= begin
    type = source_types_map[name]
    type.from_spec_args(environment, param, options)
  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
    source_from_options(definition)
  when Proc
    receiver = SourceShortcutDefinitionReceiver.new(self)
    receiver.instance_eval(&definition)
  end
end

#to_specObject



53
54
55
# File 'lib/librarian/dsl/target.rb', line 53

def to_spec
  Spec.new(@sources, @dependencies)
end