Class: ParamsReady::Parameter::AbstractParameter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Extensions::Freezer
Includes:
Extensions::Freezer::InstanceMethods, FromHash
Defined in:
lib/params_ready/parameter/parameter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Freezer

freeze_variable, freeze_variables, variables_to_freeze

Methods included from FromHash

#set_from_hash

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

#initialize(definition, **options) ⇒ AbstractParameter

Returns a new instance of AbstractParameter.

Raises:



136
137
138
139
# File 'lib/params_ready/parameter/parameter.rb', line 136

def initialize(definition, **options)
  raise ParamsReadyError, "Unexpected options: #{options}" unless options.empty?
  @definition = definition
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



104
105
106
# File 'lib/params_ready/parameter/parameter.rb', line 104

def definition
  @definition
end

Class Method Details

.intent_for_children(method, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/params_ready/parameter/parameter.rb', line 112

def self.intent_for_children(method, &block)
  case method
  when :restriction
    raise ParamsReadyError, "Block unexpected for '#{method}' method" unless block.nil?
    define_method :intent_for_children do |intent|
      intent.for_children(self)
    end
  when :delegate
    define_method :intent_for_children do |intent|
      delegate_name, *others = self.instance_eval(&block)
      intent.delegate(self, delegate_name, *others)
    end
  when :pass
    raise ParamsReadyError, "Block unexpected for '#{method}' method" unless block.nil?
    define_method :intent_for_children do |intent|
      intent
    end
  else
    raise ParamsReadyError, "Unimplemented permission method: '#{method}'"
  end
end

Instance Method Details

#==(other) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/params_ready/parameter/parameter.rb', line 176

def ==(other)
  return false unless self.match?(other)

  bare_value == other.bare_value
rescue
  false
end

#dupObject



196
197
198
199
200
# File 'lib/params_ready/parameter/parameter.rb', line 196

def dup
  clone = definition.create
  populate_other clone
  clone
end

#inspectObject



190
191
192
193
194
# File 'lib/params_ready/parameter/parameter.rb', line 190

def inspect
  preserve = Format.instance(:inspect).preserve?(self)
  content = preserve ? inspect_content : '[FILTERED]'
  "#{self.class.name.split("::").last} #{self.name}: { #{content} }"
end

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
# File 'lib/params_ready/parameter/parameter.rb', line 171

def match?(other)
  return false unless other.instance_of?(self.class)
  definition == other.definition
end

#populate(context, validator) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/params_ready/parameter/parameter.rb', line 156

def populate(context, validator)
  return if definition.populator.nil?

  definition.populator.call(context, self)
  validator
rescue => error
  populator_error = PopulatorError.new(error)
  if validator.nil?
    raise populator_error
  else
    validator.error! populator_error
  end
  validator
end

#to_hash(format = Format.instance(:backend), restriction: nil, data: nil) ⇒ Object



184
185
186
187
188
# File 'lib/params_ready/parameter/parameter.rb', line 184

def to_hash(format = Format.instance(:backend), restriction: nil, data: nil)
  restriction ||= Restriction.blanket_permission
  intent = Intent.new(format, restriction, data: data)
  to_hash_if_eligible(intent) || {}
end

#update_if_applicable(value, path) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/params_ready/parameter/parameter.rb', line 146

def update_if_applicable(value, path)
  if path.empty?
    update_self(value)
  elsif respond_to? :update_child
    update_child(value, path)
  else
    raise ParamsReadyError, "Expected path to be terminated in '#{name}'"
  end
end

#update_in(value, path) ⇒ Object



141
142
143
144
# File 'lib/params_ready/parameter/parameter.rb', line 141

def update_in(value, path)
  _, updated = update_if_applicable(value, path)
  updated
end