Class: Veto::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, conditions_context = {}, &block) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
# File 'lib/veto/builder.rb', line 9

def initialize context, conditions_context={}, &block
	@context = context
	@conditions_context = conditions_context
	instance_eval(&block) if block_given?
end

Instance Attribute Details

#conditions_contextObject (readonly)

Returns the value of attribute conditions_context.



7
8
9
# File 'lib/veto/builder.rb', line 7

def conditions_context
  @conditions_context
end

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/veto/builder.rb', line 7

def context
  @context
end

Instance Method Details

#validate(*method_names) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/veto/builder.rb', line 45

def validate *method_names
	if method_names.last.is_a?(Hash)
		c = method_names.last
		mns = method_names[0..-2]
	else
		c = {}
		mns = method_names
	end

	mc = ::Veto::Conditions.merge(conditions_context, c)
	vs = mns.map do |mn|
		::Veto::CustomMethodValidator.new(mn, mc)
	end
	validate_with vs
end

#validate_with(*args) ⇒ Object



61
62
63
# File 'lib/veto/builder.rb', line 61

def validate_with *args
	context.validate_with *args
end

#validates(attribute, options = {}) ⇒ Object



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

def validates attribute, options={}
	a = attribute
	c = ::Veto::Conditions.select(options)
	mc = ::Veto::Conditions.merge(conditions_context, c)
	o = ::Veto::Conditions.reject(options)
	vs = o.map do |t, vopts|
		vo = case vopts
		when TrueClass
			{}
		when Hash
			vopts
		when Range, Array
			{ :in => vopts }
		else
			{ :with => vopts }
		end

		vc = ::Veto::Conditions.select(vo)
		mvc = ::Veto::Conditions.merge(mc, vc)
		mvo = vo.merge(mvc)
		::Veto::AttributeValidatorFactory.new_validator(t, a, mvo)
	end
	validate_with vs
end

#with_options(conditions = {}, &block) ⇒ Object



15
16
17
18
# File 'lib/veto/builder.rb', line 15

def with_options conditions={}, &block
	mc = ::Veto::Conditions.merge(conditions_context, conditions)
	::Veto::Builder.new(context, mc, &block)
end