Class: ParamsReady::Parameter::AbstractDefinition
Instance Attribute Summary collapse
Instance Method Summary
collapse
freeze_variable, freeze_variables, variables_to_freeze
obligatory, obligatory!
class_reader_writer
late_init
collection
#freeze
Constructor Details
Returns a new instance of AbstractDefinition.
31
32
33
34
|
# File 'lib/params_ready/parameter/definition.rb', line 31
def initialize(name, altn: nil)
@name = name.to_sym
@altn = normalize_alternative_name(altn || name)
end
|
Instance Attribute Details
#altn ⇒ Object
Returns the value of attribute altn.
25
26
27
|
# File 'lib/params_ready/parameter/definition.rb', line 25
def altn
@altn
end
|
#name ⇒ Object
Returns the value of attribute name.
25
26
27
|
# File 'lib/params_ready/parameter/definition.rb', line 25
def name
@name
end
|
Instance Method Details
#create ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/params_ready/parameter/definition.rb', line 47
def create
raise ParamsReadyError, "Can't create '#{name}' using unfrozen definition" unless frozen?
instance = parameter_class.new self
if @helpers
singleton = class << instance; self; end
@helpers.each do |(name, block)|
if singleton.method_defined? name
raise ParamsReadyError, "Helper '#{name}' overrides existing method"
end
singleton.send :define_method, name, &block
end
end
instance
end
|
#finish ⇒ Object
78
79
80
81
|
# File 'lib/params_ready/parameter/definition.rb', line 78
def finish
super
freeze
end
|
#from_hash(hash, context: nil, validator: Result.new(name)) ⇒ Object
63
64
65
66
67
68
|
# File 'lib/params_ready/parameter/definition.rb', line 63
def from_hash(hash, context: nil, validator: Result.new(name))
context = InputContext.resolve(context)
instance = create
result = instance.set_from_hash(hash, context: context, validator: validator)
[result, instance]
end
|
70
71
72
73
74
75
76
|
# File 'lib/params_ready/parameter/definition.rb', line 70
def from_input(input, context: nil, validator: nil)
validator ||= Result.new(name)
context = InputContext.resolve(context)
instance = create
result = instance.set_from_input(input, context, validator)
[result, instance]
end
|
#normalize_alternative_name(name) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/params_ready/parameter/definition.rb', line 36
def normalize_alternative_name(name)
if name.is_a? Array
name.map(&:to_sym)
else
name.to_sym
end
end
|
#parameter_class ⇒ Object
27
28
29
|
# File 'lib/params_ready/parameter/definition.rb', line 27
def parameter_class
self.class.parameter_class
end
|