Class: CPUInfo::CPUS::CPU

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

Defined Under Namespace

Classes: CPUFlags

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cpuinfo) ⇒ CPU

Returns a new instance of CPU.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cpuinfo.rb', line 23

def initialize(cpuinfo)
    @attributes = {}
    cpuinfo.lines.each do |line|
        matches = /^([^:]+):(.+)\n/.match(line)
        next unless matches
        attribute = matches[1].strip.gsub(/[^a-zA-Z_]/,'_')
        value = matches[2].strip
        if value =~ /^[0-9]+(\.0*)?$/
            value = value.to_i
        elsif value =~ /^[0-9\.]$/
            value = value.to_f
        elsif ['no', 'false'].include? value.downcase
            value = false
        elsif ['yes', 'true'].include? value.downcase
            value = true
        elsif attribute == 'flags'
            value = CPUFlags.new value
        end
        @attributes[attribute.to_sym] = value
    end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *parameters) ⇒ Object



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

def method_missing(sym, *parameters)
    return @attributes[sym.to_sym] if parameters.empty? && @attributes.key?(sym.to_sym)
    @attributes[:flags].public_send(sym, *parameters)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end