Class: ConfSources::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/confsources.rb

Direct Known Subclasses

Clone, CommandLine, File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefault


16
17
18
19
20
# File 'lib/confsources.rb', line 16

def initialize
    @options = ConfMaker::get_options.collect{|e| e.clone }
    #Some default options may be not valid
    #validate!
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


15
16
17
# File 'lib/confsources.rb', line 15

def options
  @options
end

Instance Method Details

#[](arg) ⇒ Object


28
29
30
# File 'lib/confsources.rb', line 28

def [] arg
    @options.select{ |opt| opt[:name] == arg }.first
end

#export_to(file) ⇒ Object

Export into bash environment script


49
50
51
52
53
54
55
56
57
# File 'lib/confsources.rb', line 49

def export_to file
    ::File.open(file,"w+") { |f|
        @options.each { |opt|
            opt.get.each_pair { |k,v|
                f.puts "#{k.to_s}='#{v.to_s}'"
            }
        }
    }
end

#is_defined?(opt_name) ⇒ Boolean


62
63
64
# File 'lib/confsources.rb', line 62

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!


21
22
23
24
# File 'lib/confsources.rb', line 21

def merge other_one
    options = @options.collect { |el| (other_one.is_defined? el[:name]) ? other_one[el[:name]] : el }
    Clone.new options
end

#merge!(other_one) ⇒ Object


25
26
27
# File 'lib/confsources.rb', line 25

def merge! other_one
    @options.collect! { |el| (other_one.is_defined? el[:name]) ? other_one[el[:name]].clone : el }
end

#to_aObject


42
43
44
# File 'lib/confsources.rb', line 42

def to_a
    @options.collect{ |opt| opt.to_h }
end

#to_pairsObject


45
46
47
# File 'lib/confsources.rb', line 45

def to_pairs
    @options.collect{ |opt| opt.to_pair }.to_h
end

#to_sObject

Human-readable description


59
60
61
# File 'lib/confsources.rb', line 59

def to_s
    ("Configuration:\n" + @options.collect {|opt| opt.to_s}.join("\n")).gsub("\n","\n\t")
end

#validate!Object


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/confsources.rb', line 31

def validate!
    @options.each { |el|
        unless @options.select{ |opt| opt[:name] == el[:name] }.count == 1
            raise ArgumentError, "Options with name #{el[:name]} duplicated"
        end
        unless @options.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