Class: Confection::Store
- Inherits:
-
Object
- Object
- Confection::Store
- Includes:
- Enumerable
- Defined in:
- lib/confection/store.rb
Instance Attribute Summary collapse
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Instance Method Summary collapse
-
#<<(conf) ⇒ Object
Add as configuratio to the store.
-
#clear! ⇒ Object
Clear configs.
-
#concat(configs) ⇒ Object
Add a list of configs.
-
#each(&block) ⇒ Object
Iterate over each configurations.
- #first ⇒ Object
-
#import(tool, profile, options, &block) ⇒ Object
Import configuration from another project.
-
#initialize(*sources) ⇒ Store
constructor
A new instance of Store.
- #last ⇒ Object
-
#lookup(tool, profile = nil) ⇒ Object
Lookup configuration by tool and profile name.
- #parse(file) ⇒ Object
-
#profiles(tool) ⇒ Object
Returns list of profiles collected from all configs.
-
#size ⇒ Fixnum
The number of configurations.
Constructor Details
#initialize(*sources) ⇒ Store
Returns a new instance of Store.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/confection/store.rb', line 26 def initialize(*sources) @sources = sources @list = [] sources.each do |source| if File.file?(source) parse(source) else # ignore directories end end end |
Instance Attribute Details
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
41 42 43 |
# File 'lib/confection/store.rb', line 41 def sources @sources end |
Instance Method Details
#<<(conf) ⇒ Object
Add as configuratio to the store.
67 68 69 70 |
# File 'lib/confection/store.rb', line 67 def <<(conf) raise TypeError, "not a configuration instance -- `#{conf}'" unless Config === conf @list << conf end |
#clear! ⇒ Object
Clear configs.
112 113 114 |
# File 'lib/confection/store.rb', line 112 def clear! @list = [] end |
#concat(configs) ⇒ Object
Add a list of configs.
75 76 77 |
# File 'lib/confection/store.rb', line 75 def concat(configs) configs.each{ |c| self << c } end |
#each(&block) ⇒ Object
Iterate over each configurations.
51 52 53 |
# File 'lib/confection/store.rb', line 51 def each(&block) @list.each(&block) end |
#first ⇒ Object
117 118 119 |
# File 'lib/confection/store.rb', line 117 def first @list.first end |
#import(tool, profile, options, &block) ⇒ Object
Import configuration from another project.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/confection/store.rb', line 140 def import(tool, profile, , &block) from_tool = [:tool] || tool from_profile = [:profile] || profile case from = [:from] when String, Symbol project = Project.load(from.to_s) store = project ? project.store : nil else from = '(self)' store = self end raise "no configuration found in `#{from}'" unless store configs = store.lookup(from_tool, from_profile) configs.each do |config| new_config = config.copy(:tool=>tool, :profile=>profile) #new_options = @_options.dup #new_options[:tool] = tool #new_options[:profile] = profile #new_options[:block] = config.block #new_options[:text] = config.text # not so sure about this one if String === new_config.value new_config.value += ("\n" + [:text].to_s) if [:text] end self << new_config end #if block # self << Config::Block.new(tool, profile, nil, &block) #end end |
#last ⇒ Object
122 123 124 |
# File 'lib/confection/store.rb', line 122 def last @list.last end |
#lookup(tool, profile = nil) ⇒ Object
TODO:
Future versions should allow this to handle regex and fnmatches.
Lookup configuration by tool and profile name.
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/confection/store.rb', line 84 def lookup(tool, profile=nil) if profile == '*' select do |c| c.tool.to_sym == tool.to_sym end else profile = profile.to_sym if profile select do |c| c.tool.to_sym == tool.to_sym && c.profile == profile end end end |
#parse(file) ⇒ Object
44 45 46 |
# File 'lib/confection/store.rb', line 44 def parse(file) DSL.parse(self, file) end |
#profiles(tool) ⇒ Object
Returns list of profiles collected from all configs.
101 102 103 104 105 106 107 |
# File 'lib/confection/store.rb', line 101 def profiles(tool) names = [] each do |c| names << c.profile if c.tool == tool.to_sym end names.uniq end |
#size ⇒ Fixnum
The number of configurations.
60 61 62 |
# File 'lib/confection/store.rb', line 60 def size @list.size end |