Class: HashValidator::DSLProxy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ DSLProxy

Returns a new instance of DSLProxy.



113
114
115
# File 'lib/hash_validator/hash_validator.rb', line 113

def initialize(subject)
  @subject = subject
end

Instance Attribute Details

#subjectObject (readonly)

Returns the value of attribute subject.



111
112
113
# File 'lib/hash_validator/hash_validator.rb', line 111

def subject
  @subject
end

Instance Method Details

#allowed_values(ary) ⇒ Object



144
145
146
# File 'lib/hash_validator/hash_validator.rb', line 144

def allowed_values(ary)
  @subject.add_option(:allowed_values, ary)
end

#default(hsh) ⇒ Object

Raises:

  • (ArgumentError)


152
153
154
155
# File 'lib/hash_validator/hash_validator.rb', line 152

def default(hsh)
  raise ArgumentError unless hsh.is_a?(Hash)
  @subject.default_value = hsh
end

#forbidden_values(ary) ⇒ Object



148
149
150
# File 'lib/hash_validator/hash_validator.rb', line 148

def forbidden_values(ary)
  @subject.add_option(:forbidden_values, ary)
end

#key(k, opts = {}, &block) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/hash_validator/hash_validator.rb', line 117

def key(k, opts = {}, &block)
  opts.reverse_merge!({
    type: String
  })

  @subject.key(k, opts, &block)
end

#optional(*args, &block) ⇒ Object



125
126
127
128
129
130
# File 'lib/hash_validator/hash_validator.rb', line 125

def optional(*args, &block)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  args.each do |key|
    key(key, opts.merge({required: false}), &block)
  end
end

#required(*args, &block) ⇒ Object



132
133
134
135
136
137
# File 'lib/hash_validator/hash_validator.rb', line 132

def required(*args, &block)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  args.each do |key|
    key(key, opts.merge({required: true}), &block)
  end
end

#type(klass) ⇒ Object

Raises:

  • (ArgumentError)


139
140
141
142
# File 'lib/hash_validator/hash_validator.rb', line 139

def type(klass)
  raise ArgumentError unless klass.is_a?(Class)
  @subject.add_option(:type, klass)
end