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

Returns a new instance of Default.



18
19
20
21
22
# File 'lib/confsources.rb', line 18

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.



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

def options
  @options
end

Instance Method Details

#[](arg) ⇒ Object



30
31
32
# File 'lib/confsources.rb', line 30

def [] arg
    @options.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|
        @options.each { |opt|
            opt.get.each_pair { |k,v|
                f.puts "#{k.to_s}='#{v.to_s}'"
            }
        }
    }
end

#is_defined?(opt_name) ⇒ Boolean

Returns:

  • (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
    options = @options.collect { |el| (other_one.is_defined? el[:name]) ? other_one[el[:name]] : el }
    Clone.new options
end

#merge!(other_one) ⇒ Object



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

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

#to_aObject

array of option hashes (including names, desc and so on)



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

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

#to_hObject

hash with parsed values



49
50
51
# File 'lib/confsources.rb', line 49

def to_h
    @options.inject({}){|rez,opt| rez.merge opt.get}
end

#to_pairsObject

:name => :value hash



53
54
55
# File 'lib/confsources.rb', line 53

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

#to_sObject

Human-readable description



67
68
69
# File 'lib/confsources.rb', line 67

def to_s
    ("Configuration:\n" + @options.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!
    @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