Class: Eye::Dsl::PureOpts

Inherits:
Object show all
Defined in:
lib/eye/dsl/pure_opts.rb

Direct Known Subclasses

ConfigOpts, Opts

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, parent = nil, merge_parent_config = true) ⇒ PureOpts

Returns a new instance of PureOpts.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/eye/dsl/pure_opts.rb', line 46

def initialize(name = nil, parent = nil, merge_parent_config = true)
  @name = name.to_s
  @full_name = @name

  if parent
    @parent = parent
    if merge_parent_config
      @config = Eye::Utils.deep_clone(parent.config)
      parent.not_seed_options.each { |opt| @config.delete(opt) }
    else
      @config = {}
    end
    @full_name = "#{parent.full_name}:#{@full_name}"
  else
    @config = {}
  end

  @config[:name] = @name if @name.present?
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



44
45
46
# File 'lib/eye/dsl/pure_opts.rb', line 44

def config
  @config
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



43
44
45
# File 'lib/eye/dsl/pure_opts.rb', line 43

def full_name
  @full_name
end

#nameObject (readonly)

Returns the value of attribute name.



43
44
45
# File 'lib/eye/dsl/pure_opts.rb', line 43

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



44
45
46
# File 'lib/eye/dsl/pure_opts.rb', line 44

def parent
  @parent
end

Class Method Details

.create_options_methods(arr, types = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eye/dsl/pure_opts.rb', line 3

def self.create_options_methods(arr, types = nil)
  m = Module.new do
    arr.each do |opt|
      define_method("set_#{opt}") do |arg|
        key = opt.to_sym

        if (disallow_options && disallow_options.include?(key)) || (allow_options && !allow_options.include?(key))
          raise Eye::Dsl::Error, "disallow option #{key} for #{self.class.inspect}"
        end

        if types
          good_type = Array(types).any? { |type| arg.is_a?(type) } || arg.nil?
          raise Eye::Dsl::Error, "bad :#{opt} value #{arg.inspect}, type should be #{types.inspect}" unless good_type
        end

        @config[key] = arg
      end

      define_method("get_#{opt}") do
        @config[opt.to_sym]
      end

      define_method(opt) do |*args|
        if args.blank?
          # getter
          send "get_#{opt}"
        else
          send "set_#{opt}", *args
        end
      end

      define_method("#{opt}=") do |arg|
        send opt, arg
      end
    end
  end

  send :include, m
end

.with_parsed_file(file_name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/eye/dsl/pure_opts.rb', line 104

def self.with_parsed_file(file_name)
  saved_parsed_filename = Eye.parsed_filename

  real_filename = if Eye.parsed_filename && File.symlink?(Eye.parsed_filename)
    File.readlink(Eye.parsed_filename)
  else
    Eye.parsed_filename
  end

  dirname = File.dirname(real_filename) rescue nil
  path = File.expand_path(file_name, dirname)

  Eye.parsed_filename = path
  yield path
ensure
  Eye.parsed_filename = saved_parsed_filename
end

Instance Method Details

#allow_optionsObject



66
67
68
# File 'lib/eye/dsl/pure_opts.rb', line 66

def allow_options
  nil
end

#disallow_optionsObject



70
71
72
# File 'lib/eye/dsl/pure_opts.rb', line 70

def disallow_options
  []
end

#nop(*args, &block) ⇒ Object



102
# File 'lib/eye/dsl/pure_opts.rb', line 102

def nop(*args, &block); end

#not_seed_optionsObject



74
75
76
# File 'lib/eye/dsl/pure_opts.rb', line 74

def not_seed_options
  []
end

#use(proc, *args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/eye/dsl/pure_opts.rb', line 82

def use(proc, *args)
  if proc.is_a?(String)
    self.class.with_parsed_file(proc) do |path|
      if File.exist?(path)
        Eye::Dsl.debug { "=> load #{path}" }
        instance_eval(File.read(path))
        Eye::Dsl.debug { "<= load #{path}" }
      end
    end
  else
    ie = if args.present?
      ->(i) { proc[i, *args] }
    else
      proc
    end

    instance_eval(&ie)
  end
end

#with_condition(cond = true, &block) ⇒ Object



78
79
80
# File 'lib/eye/dsl/pure_opts.rb', line 78

def with_condition(cond = true, &block)
  instance_eval(&block) if cond && block
end