Class: ParamsReady::Parameter::AbstractDefinition

Inherits:
Object
  • Object
show all
Extended by:
Extensions::ClassReaderWriter, Extensions::Collection, Extensions::Finalizer, Extensions::Freezer, Extensions::LateInit
Includes:
Extensions::Finalizer::InstanceMethods, Extensions::Freezer::InstanceMethods
Defined in:
lib/params_ready/parameter/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Freezer

freeze_variable, freeze_variables, variables_to_freeze

Methods included from Extensions::Finalizer

obligatory, obligatory!

Methods included from Extensions::ClassReaderWriter

class_reader_writer

Methods included from Extensions::LateInit

late_init

Methods included from Extensions::Collection

collection

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

#initialize(name, altn: nil) ⇒ AbstractDefinition

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

#altnObject (readonly)

Returns the value of attribute altn.



25
26
27
# File 'lib/params_ready/parameter/definition.rb', line 25

def altn
  @altn
end

#nameObject (readonly)

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

#createObject

Raises:



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

#finishObject



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

#from_input(input, context: nil, validator: nil) ⇒ Object



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_classObject



27
28
29
# File 'lib/params_ready/parameter/definition.rb', line 27

def parameter_class
  self.class.parameter_class
end