Class: Dply::BaseConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/dply/base_config.rb

Direct Known Subclasses

BuildConfig, DeployConfig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseConfig

Returns a new instance of BaseConfig.



84
85
86
87
# File 'lib/dply/base_config.rb', line 84

def initialize
  @opts_struct = self.class.struct_klass.new
  default_config
end

Instance Attribute Details

#opts_structObject (readonly)

Returns the value of attribute opts_struct.



82
83
84
# File 'lib/dply/base_config.rb', line 82

def opts_struct
  @opts_struct
end

Class Method Details

.build(path = nil, &block) ⇒ Object



73
74
75
76
77
78
# File 'lib/dply/base_config.rb', line 73

def self.build(path = nil, &block)
  i = new
  i.read(path) if path
  i.instance_eval(&block) if block_given?
  i.opts_struct
end

.define_opt(opts) ⇒ Object



15
16
17
18
19
# File 'lib/dply/base_config.rb', line 15

def self.define_opt(opts)
  opts.define_singleton_method(:opt) do |name, type: Object|
    self[name] = type
  end
end

.define_opt_setter(opt) ⇒ Object



63
64
65
66
67
# File 'lib/dply/base_config.rb', line 63

def self.define_opt_setter(opt)
  define_method(opt) do |value = nil, &block|
    set opt, value, &block
  end
end

.define_opts(&block) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/dply/base_config.rb', line 5

def self.define_opts(&block)
  opts = {}
  define_opt(opts)
  opts.instance_eval(&block)
  @struct_klass = new_struct_klass(opts)
  opts.each do |opt, _|
    define_opt_setter(opt)
  end
end

.new_struct_klass(opts) ⇒ Object



21
22
23
24
25
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
57
58
59
60
61
# File 'lib/dply/base_config.rb', line 21

def self.new_struct_klass(opts)
  Class.new do

    def initialize
      @proc_map = {}
    end

    def set_proc(opt, pr)
      @proc_map[opt] = pr
      ivar = "@#{opt}"
      remove_instance_variable(ivar) if instance_variable_defined?(ivar)
    end

    opts.each do |opt, type|

      define_method "#{opt}=" do |value|
        assert_type(opt, value, type)
        instance_variable_set("@#{opt}", value)
      end

      define_method opt do
        if instance_variable_defined?("@#{opt}")
          instance_variable_get("@#{opt}")
        elsif @proc_map.key?(opt)
          value = instance_eval(&@proc_map.fetch(opt))
          public_send("#{opt}=", value)
        else
          instance_variable_set("@#{opt}", nil)
        end
      end
    end

    opts = nil

    private def assert_type(opt, value, type)
      if not value.is_a?(type)
        raise Error, "opt(#{opt}) has value '#{value}' of type '#{value.class}' (Expected: '#{type}')"
      end
    end
  end
end

.struct_klassObject



69
70
71
# File 'lib/dply/base_config.rb', line 69

def self.struct_klass
  @struct_klass
end

Instance Method Details

#default_configObject



89
90
# File 'lib/dply/base_config.rb', line 89

def default_config
end

#read(path, optional: false) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/dply/base_config.rb', line 100

def read(path, optional: false)
  if not File.readable?(path)
    return if optional
    raise Error, "config #{path} not readable"
  end
  instance_eval(File.read(path), path)
rescue NoMethodError => e
  raise Error, "invalid option used in config: #{e.name}"
end

#set(opt, value, &block) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/dply/base_config.rb', line 92

def set(opt, value, &block)
  if block
    @opts_struct.set_proc(opt, block)
  else
    @opts_struct.public_send("#{opt}=", value)
  end
end