Class: BinData::RecordArgExtractor

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

Overview

Extracts args for Records.

Foo.new(:bar => “baz) is ambiguous as to whether :bar is a value or parameter.

BaseArgExtractor always assumes :bar is parameter. This extractor correctly identifies it as value or parameter.

Class Method Summary collapse

Class Method Details

.extract(the_class, the_args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/bindata/record.rb', line 14

def extract(the_class, the_args)
  value, parameters, parent = BaseArgExtractor.extract(the_class, the_args)

  if parameters_is_value?(the_class, value, parameters)
    value = parameters
    parameters = {}
  end

  [value, parameters, parent]
end

.field_names_in_parameters?(the_class, parameters) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/bindata/record.rb', line 33

def field_names_in_parameters?(the_class, parameters)
  field_names = the_class.fields.field_names
  param_keys = parameters.keys

  (field_names & param_keys).length > 0
end

.parameters_is_value?(the_class, value, parameters) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/bindata/record.rb', line 25

def parameters_is_value?(the_class, value, parameters)
  if value.nil? and parameters.length > 0
    field_names_in_parameters?(the_class, parameters)
  else
    false
  end
end