Class: ConfSources::Default
- Inherits:
-
Object
- Object
- ConfSources::Default
- Defined in:
- lib/confsources.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #[](arg) ⇒ Object
-
#export_to(file) ⇒ Object
Export into bash environment script.
-
#initialize ⇒ Default
constructor
A new instance of Default.
- #is_defined?(opt_name) ⇒ Boolean
-
#merge(other_one) ⇒ Object
Some default options may be not valid validate!.
- #merge!(other_one) ⇒ Object
-
#to_a ⇒ Object
array of option hashes (including names, desc and so on).
-
#to_h ⇒ Object
hash with parsed values.
-
#to_pairs ⇒ Object
:name => :value hash.
-
#to_s ⇒ Object
Human-readable description.
- #validate! ⇒ Object
Constructor Details
#initialize ⇒ Default
Returns a new instance of Default.
18 19 20 21 22 |
# File 'lib/confsources.rb', line 18 def initialize = ConfMaker::.collect{|e| e.clone } #Some default options may be not valid #validate! end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/confsources.rb', line 17 def end |
Instance Method Details
#[](arg) ⇒ Object
30 31 32 |
# File 'lib/confsources.rb', line 30 def [] arg .select{ |opt| opt[:name] == arg }.first end |
#export_to(file) ⇒ Object
Export into bash environment script
57 58 59 60 61 62 63 64 65 |
# File 'lib/confsources.rb', line 57 def export_to file ::File.open(file,"w+") { |f| .each { |opt| opt.get.each_pair { |k,v| f.puts "#{k.to_s}='#{v.to_s}'" } } } end |
#is_defined?(opt_name) ⇒ Boolean
70 71 72 |
# File 'lib/confsources.rb', line 70 def is_defined? opt_name self[opt_name].kind_of? ConfOptions::Standard end |
#merge(other_one) ⇒ Object
Some default options may be not valid validate!
23 24 25 26 |
# File 'lib/confsources.rb', line 23 def merge other_one = .collect { |el| (other_one.is_defined? el[:name]) ? other_one[el[:name]] : el } Clone.new end |
#merge!(other_one) ⇒ Object
27 28 29 |
# File 'lib/confsources.rb', line 27 def merge! other_one .collect! { |el| (other_one.is_defined? el[:name]) ? other_one[el[:name]].clone : el } end |
#to_a ⇒ Object
array of option hashes (including names, desc and so on)
45 46 47 |
# File 'lib/confsources.rb', line 45 def to_a .collect{ |opt| opt.to_h } end |
#to_h ⇒ Object
hash with parsed values
49 50 51 |
# File 'lib/confsources.rb', line 49 def to_h .inject({}){|rez,opt| rez.merge opt.get} end |
#to_pairs ⇒ Object
:name => :value hash
53 54 55 |
# File 'lib/confsources.rb', line 53 def to_pairs .collect{ |opt| opt.to_pair }.to_h end |
#to_s ⇒ Object
Human-readable description
67 68 69 |
# File 'lib/confsources.rb', line 67 def to_s ("Configuration:\n" + .collect {|opt| opt.to_s}.join("\n")).gsub("\n","\n\t") end |
#validate! ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/confsources.rb', line 33 def validate! .each { |el| unless .select{ |opt| opt[:name] == el[:name] }.count == 1 raise ArgumentError, "Options with name #{el[:name]} duplicated" end unless .select{ |opt| opt[:name] == el[:name] }.count == 1 raise ArgumentError, "Option #{el[:name]} duplicates aliases with #{opt[:name]}" end el.validate! Clone.new(self) } end |