Class: DRbQS::Setting::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/drbqs/setting/source.rb,
lib/drbqs/setting/data_container.rb

Defined Under Namespace

Classes: DataContainer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_keys_defined = true) ⇒ Source

Returns a new instance of Source.



9
10
11
12
13
14
15
# File 'lib/drbqs/setting/source.rb', line 9

def initialize(all_keys_defined = true)
  @cond = {}
  @default = {}
  @value = DRbQS::Setting::Source::DataContainer.new(Array)
  @argument_condition = nil
  @all_keys_defined = all_keys_defined
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/drbqs/setting/source.rb', line 7

def default
  @default
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/drbqs/setting/source.rb', line 7

def value
  @value
end

Class Method Details

.clone_container(obj) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/drbqs/setting/data_container.rb', line 29

def self.clone_container(obj)
  cl = DRbQS::Setting::Source::DataContainer.new(obj.__array__)
  cl.argument = obj.argument.clone
  obj.__data__.each do |key, val|
    cl.__data__[key] = val.clone
  end
  cl
end

Instance Method Details

#check!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/drbqs/setting/source.rb', line 64

def check!
  if @argument_condition
    check_argument_array_size(@argument_condition, get_argument, "argument array")
  end
  keys = @value.__data__.keys
  keys.each do |key|
    args = @value.__data__[key]
    unless Symbol === key
      key = key.intern
      @value.__data__.delete(key)
    end
    if registered_key?(key)
      if check = @cond[key][:check]
        check_argument_array_size(check, args, key)
      end
    elsif @all_keys_defined
      raise DRbQS::Setting::InvalidArgument, "Undefined key '#{key.inspect}' must not set."
    end
    if boolean_value?(key)
      @value.__data__[key] = [(args.size == 0 || args[0] ? true : false)]
    else
      @value.__data__[key] = args
    end
  end
end

#clear(key) ⇒ Object



139
140
141
# File 'lib/drbqs/setting/source.rb', line 139

def clear(key)
  @value.__data__.delete(key.intern)
end

#cloneObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/drbqs/setting/source.rb', line 17

def clone
  new_obj = self.class.new(!!@all_keys_defined)
  args = [@cond.clone, @default.clone, DRbQS::Setting::Source.clone_container(@value), @argument_condition]
  new_obj.instance_exec(*args) do |cond, default, value, arg_cond|
    @cond = cond
    @default = default
    @value = value
    @argument_condition = arg_cond
  end
  new_obj
end

#command_line_argument(escape = nil) ⇒ Object



216
217
218
# File 'lib/drbqs/setting/source.rb', line 216

def command_line_argument(escape = nil)
  argument_array_for_command_line(escape) + option_array_for_command_line(escape)
end

#get(key, &block) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/drbqs/setting/source.rb', line 143

def get(key, &block)
  k = key.intern
  val = @value.__data__[k] || @default[k]
  if block_given? && val
    yield(val)
  else
    val
  end
end

#get_argumentObject



168
169
170
# File 'lib/drbqs/setting/source.rb', line 168

def get_argument
  @value.argument
end

#get_first(key, &block) ⇒ Object



153
154
155
156
157
158
# File 'lib/drbqs/setting/source.rb', line 153

def get_first(key, &block)
  val = get(key) do |ary|
    ary[0]
  end
  block_given? && val ? yield(val) : val
end

#register_key(key, opts = {}) ⇒ Object

Parameters:

  • key (String, Symbol)

    Set a key of a parameter value.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :check (Array, Fixnum)

    Set the condition of value.

  • :add (Boolean)

    The value is an array to set multiple values.

  • :bool (Boolean)

    If opts[:bool] is true then the value is boolean.

  • :default (Array)

    Set the default value.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/drbqs/setting/source.rb', line 108

def register_key(key, opts = {})
  k = key.intern
  if registered_key?(k)
    @cond[k].clear
  else
    @cond[k] = {}
  end
  if check = opts[:check]
    @cond[k][:check] = parse_condition(check)
  end
  if default = opts[:default]
    check_argument_array_size(@cond[k][:check], default, "default of #{k.inspect}") if @cond[k][:check]
    @default[k] = default
  end
  @cond[k][:add] = opts[:add]
  @cond[k][:bool] = opts[:bool]
end

#registered_keysObject

For debug.



30
31
32
# File 'lib/drbqs/setting/source.rb', line 30

def registered_keys
  @cond.keys
end

#set(key, *args) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/drbqs/setting/source.rb', line 130

def set(key, *args)
  k = key.intern
  if value_to_add?(key) && @value.__data__[k]
    @value.__data__[k].concat(args)
  else
    @value.__data__[k] = args
  end
end

#set?(key) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/drbqs/setting/source.rb', line 160

def set?(key)
  !!@value.__data__[key.intern]
end

#set_argument(*args) ⇒ Object



164
165
166
# File 'lib/drbqs/setting/source.rb', line 164

def set_argument(*args)
  @value.argument = args
end

#set_argument_condition(*checks) ⇒ Object



126
127
128
# File 'lib/drbqs/setting/source.rb', line 126

def set_argument_condition(*checks)
  @argument_condition = parse_condition(checks)
end