Class: Sumodev::Sumofile

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

Defined Under Namespace

Classes: NoSuchFileError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Sumofile

Returns a new instance of Sumofile.

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
38
# File 'lib/sumodev/sumofile.rb', line 34

def initialize
  @variables = {}

  yield self if block_given?
end

Instance Attribute Details

#variablesObject

Returns the value of attribute variables.



4
5
6
# File 'lib/sumodev/sumofile.rb', line 4

def variables
  @variables
end

Class Method Details

.current_configObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/sumodev/sumofile.rb', line 18

def current_config
  data = File.read("#{temp_path}/sumofile.json")
  json = MultiJson.load data

  new do |sumofile|
    sumofile.variables = json['sumofile']
  end
rescue Errno::ENOENT
  raise NoSuchFileError
end

.from_file(path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/sumodev/sumofile.rb', line 7

def from_file(path)
  new do |sumofile|
    File.readlines(path).each do |line|
      _, name, value = line.match(/^(.+?)\s+['"]?(.+?)['"]?$/).to_a
      sumofile[name] = value
    end
  end
rescue Errno::ENOENT
  raise NoSuchFileError
end

.temp_pathObject



29
30
31
# File 'lib/sumodev/sumofile.rb', line 29

def temp_path
  File.expand_path(Sumodev::Config.get('SUMO_TEMP_PATH'));
end

Instance Method Details

#[]=(name, value) ⇒ Object



51
52
53
# File 'lib/sumodev/sumofile.rb', line 51

def []=(name, value)
  @variables[name] = value
end

#convert_into_json_fileObject



40
41
42
43
44
# File 'lib/sumodev/sumofile.rb', line 40

def convert_into_json_file
  File.open("#{self.class.temp_path}/sumofile.json", "w") do |file|
    file.write to_json
  end
end

#get(name) ⇒ Object Also known as: []



46
47
48
# File 'lib/sumodev/sumofile.rb', line 46

def get(name)
  @variables[name]
end

#to_jsonObject



55
56
57
# File 'lib/sumodev/sumofile.rb', line 55

def to_json
  MultiJson.dump({'sumofile' => @variables})
end