Class: Clian::Config::Element

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

Overview

Parse Key-Value object in YAML

Direct Known Subclasses

List, Toplevel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Element

Returns a new instance of Element.



79
80
81
82
83
84
85
86
87
# File 'lib/clian/config.rb', line 79

def initialize(hash = {})
  @original_hash = hash
  (hash || {}).each do |key, val|
    raise Clian::ConfigurationError, "config syntax error (#{key})" unless syntax.keyword?(key)
    var = syntax.instance_variable_name(key)
    obj = create_subnode(key, val)
    instance_variable_set(var, obj)
  end
end

Instance Attribute Details

#original_hashObject (readonly)

Returns the value of attribute original_hash.



89
90
91
# File 'lib/clian/config.rb', line 89

def original_hash
  @original_hash
end

Class Method Details

.create_from_yaml_file(yaml_file) ⇒ Object



50
51
52
53
# File 'lib/clian/config.rb', line 50

def self.create_from_yaml_file(yaml_file)
  yaml_string = File.open(File.expand_path(yaml_file)).read
  return create_from_yaml_string(yaml_string, yaml_file)
end

.create_from_yaml_string(yaml_string, filename = nil) ⇒ Object



55
56
57
58
# File 'lib/clian/config.rb', line 55

def self.create_from_yaml_string(yaml_string, filename = nil)
  hash = YAML.load(yaml_string, filename) || {}
  return new(hash)
end

.define_syntax(config) ⇒ Object

class General < Clian::Config::Element

define_syntax :client_id => String,
              :client_secret => String,
              :token_store => String,
              :context_store => String,
              :default_user => String

end # class General



68
69
70
71
72
73
# File 'lib/clian/config.rb', line 68

def self.define_syntax(config)
  @syntax = Syntax.new(config)
  @syntax.keyword_symbols.each do |sym|
    attr_reader sym
  end
end

.syntaxObject



75
76
77
# File 'lib/clian/config.rb', line 75

def self.syntax
  return @syntax
end

Instance Method Details

#get_value(dot_separated_string = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/clian/config.rb', line 91

def get_value(dot_separated_string = nil)
  if dot_separated_string.to_s == ""
    return original_hash
  end

  key, subkey = dot_separated_string.to_s.upcase.split(".", 2)
  subnode = get_subnode(key)

  if subnode.respond_to?(:get_value)
    return subnode.get_value(subkey)
  else
    return subnode.to_s
  end
end

#to_hashObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/clian/config.rb', line 110

def to_hash
  hash = {}
  syntax.keywords.each do |key|
    var = syntax.instance_variable_name(key)
    obj = instance_variable_get(var)
    obj = obj.respond_to?(:to_hash) ? obj.to_hash : obj.to_s
    hash[key] = obj
  end
  return hash
end

#to_yamlObject



106
107
108
# File 'lib/clian/config.rb', line 106

def to_yaml
  return self.to_hash.to_yaml
end