Module: Gear::Kuddly

Defined in:
lib/camping/gear/kuddly.rb

Overview

Namespace to hide all of the KDL Configure stuff.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

WARNINGS =
[]

Class Method Summary collapse

Class Method Details

.get_config(provided_config_file = nil) ⇒ Object

get_config searches for any kdl document inside of the root folder Then parses it, and merges the data based on the current environment.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/camping/gear/kuddly.rb', line 148

def get_config(provided_config_file = nil)

  config_file, merged_configs = provided_config_file, {}
  config_file = get_root_config_file() unless provided_config_file != nil

  # If the config file is just nil then we probably don't have one.
  return nil unless config_file != nil

  # parses then maps the kdl
  configs = map_kdl(parse_kdl(config_file))
  env = ENV['environment'] ||= "development"

  configs.each do |key, setting|
    if setting.has_key? :default && env.to_sym
      merged_configs[key] = setting[:default].merge(setting[env.to_sym])
    else
      merged_configs[key] = setting['default']
    end
  end

  merged_configs
end

.get_root_config_file(search_pattern = "config.kdl") ⇒ Object

get kdl config file



172
173
174
# File 'lib/camping/gear/kuddly.rb', line 172

def get_root_config_file(search_pattern = "config.kdl")
  Dir.glob(search_pattern).first
end

.included(mod) ⇒ Object



27
28
29
# File 'lib/camping/gear/kuddly.rb', line 27

def included(mod)
  mod.extend(ClassMethods)
end

.kdl_error_message(kdl_string = "", error_message = "", error = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/camping/gear/kuddly.rb', line 34

def kdl_error_message(kdl_string="",error_message="", error=nil)
  # parse error message to get line number and column:
  m = error_message.match( /\((\d)+:(\d)\)/ )

  if m == nil
    warn "kdl_error_message was called on a nil error message?"
    warn "message: #{error_message}"
    warn "kdl_string: #{kdl_string}"
    warn "current dir: #{Dir.pwd}"
    warn "#{error}"
    return
  end

  line = m[1].to_i
  lines = kdl_string.split( "\n" )

  em = "\n"
  em << "#{line-4}: #{lines[line-4]}\n" if (line-4) > 0 && (line-4) < lines.count
  em << "#{line-3}: #{lines[line-3]}\n" if (line-3) > 0 && (line-3) < lines.count
  em << "#{line-2}: #{lines[line-2]}\n" if (line-2) > 0 && (line-2) < lines.count
  em << "#{line-1}: #{lines[line-1]}\n" if (line-1) > 0 && (line-1) < lines.count
  em << "#{line}: #{lines[line]}\n"     if   (line)
  em << "#{line+1}: #{lines[line+1]}\n" if (line+1) > 0 && (line+1) < lines.count
  em << "#{line+2}: #{lines[line+2]}\n" if (line+2) > 0 && (line+2) < lines.count
  em << "#{line+3}: #{lines[line+3]}\n" if (line+3) > 0 && (line+3) < lines.count
  em << "#{line+4}: #{lines[line+4]}\n" if (line+4) > 0 && (line+4) < lines.count
  # em << "\n"
  WARNINGS << em
end

.map_kdl(kdl_doc = nil) ⇒ Object

Maps kdl settings. Settings Example: “‘ database

default adapter="sqlite3" database="#{database" host="localhost" max_connections=5 timeout=5000
development
test
production adapter="postgres" database="kow"

} “‘



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/camping/gear/kuddly.rb', line 106

def map_kdl(kdl_doc=nil)
  configs = {}

  if kdl_doc

    # We have a kdl document, so that's good.
    # iterate through each top level node to see what kind of data we have.
    kdl_doc.nodes.each do |d|
      config_name = d.name.to_sym
      configs[config_name] = {}

      if d.children.length > 0
        # we've got kids!
        # This node will have sub nodes with properties
        d.children.each do |en|
          env_name = en.name.to_sym
          # parse the settings for each environment
          configs[config_name][env_name] = {}
          en.properties.each do |key, value|
            configs[config_name][env_name][key.to_sym] = value.value
          end
        end
      else
        # we've got raw data, so place it into a default hash spot.
        vals = []
        if d.arguments.length > 1
          d.arguments.each { |v| vals << v.value }
        else
          vals = d.arguments.first.value
        end

        configs[config_name]['default'] = vals
      end
    end
  end

  configs
end

.parse_kdl(config_file = nil, silence_warnings = false) ⇒ Object

parses a kdl file into a kdl document Object. returns nil if it’s false. Also assumes that the file is exists. an optional silence_warnings parameter is set to false. This is used for testing.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/camping/gear/kuddly.rb', line 69

def parse_kdl(config_file = nil, silence_warnings = false)
  begin
    kdl_string = File.open(config_file).read
  rescue => error # Errno::ENOENT
    puts ""
    puts "Error trying to read a config file: \"#{error}.\""
    puts "  Attempted to open: #{config_file}"
    puts "  Current directory: #{Dir.pwd}"
    puts "  files in directory: #{Dir.glob('*')}"
    puts ""
  end

  begin
    kdl_doc = KDL.parse_document(kdl_string)
  rescue => error
    warn "#{error}"
    # parse error message to get line number and column:
    message = Kuddly.kdl_error_message(kdl_string, error.message, error)
    m = error.message.match( /\((\d)+:(\d)\)/ )

    line, column = m[1].to_i, m[2].to_i if m.respond_to? '[]'

    warn("\nError parsing config: #{config_file}, on line: #{line}, at column: #{column}.", message, "#{error.message}", uplevel: 1) unless silence_warnings
  end

  kdl_doc
end

.setup(app, *a, &block) ⇒ Object

required for compliance reasons



32
# File 'lib/camping/gear/kuddly.rb', line 32

def setup(app, *a, &block) end