Module: StrongYAML::ClassMethods

Defined in:
lib/strong_yaml.rb

Instance Method Summary collapse

Instance Method Details

#create_or_loadObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/strong_yaml.rb', line 46

def create_or_load
  if File.exist? @config_location
    load
  elsif @schema
    File.write(@config_location, hash_from_schema.to_yaml)

    load
  else
    FileUtils.touch(@config_location)
  end
end

#file(config_location) ⇒ Object



13
14
15
# File 'lib/strong_yaml.rb', line 13

def file(config_location)
  @config_location = config_location
end

#loadObject



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

def load
  @config = YAML.load_file(@config_location)

  if @schema
    @schema.elements.each do |schema_entry|
      obj = build_schema_entry schema_entry, @config, []

      define_singleton_method(schema_entry.name) do
        obj
      end
    end
  else
    @config.each do |key, value|
      define_singleton_method(key) do
        if value.is_a? Hash
          JSON.parse value.to_json, object_class: OpenStruct
        else
          value
        end
      end
    end
  end
end

#schema(&block) ⇒ Object



17
18
19
20
# File 'lib/strong_yaml.rb', line 17

def schema(&block)
  @schema = StrongYAML::ConfigSchema.new
  @schema.instance_eval(&block)
end