Module: Sting::StingOperations

Included in:
Sting, Sting
Defined in:
lib/sting/sting_operations.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sting/sting_operations.rb', line 26

def method_missing(name, *args, &blk)
  name = name.to_s
  return settings[name] if has_key? name

  suffix = nil

  if name.end_with? *['=', '!', '?']
    suffix = name[-1]
    name = name[0..-2]
  end

  case suffix
  when "="
    settings[name] = args.first

  when "?"
    !!settings[name]

  when "!"
    if has_key? name
      return settings[name]
    else
      raise ArgumentError, "Key '#{name}' does not exist"
    end

  else
    nil

  end

end

Instance Method Details

#<<(source) ⇒ Object Also known as: push



10
11
12
13
14
15
16
17
18
19
# File 'lib/sting/sting_operations.rb', line 10

def <<(source)
  if source.is_a? Hash
    content = source.collect{ |k,v| [k.to_s, v] }.to_h
  else
    source = "#{source}.yml" unless source =~ /\.ya?ml$/
    content = File.read source
    content = YAML.load(ERB.new(content).result)
  end
  settings.merge! content if content
end

#[](key) ⇒ Object



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

def [](key)
  settings[key.to_s]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/sting/sting_operations.rb', line 66

def has_key?(key)
  settings.has_key? key.to_s
end

#initialize(source = nil) ⇒ Object



6
7
8
# File 'lib/sting/sting_operations.rb', line 6

def initialize(source = nil)
  self << source if source
end

#reset!Object



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

def reset!
  @settings = nil
end

#settingsObject



58
59
60
# File 'lib/sting/sting_operations.rb', line 58

def settings
  @settings ||= {}
end