Class: OdataDuty::MapperBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/odata_duty/mapper_builder.rb

Constant Summary collapse

ERB_TEMPLATE =
ERB.new(File.read("#{__dir__}/dynamic_object_wrapper.rb.erb"), trim_mode: '<>')
VALID_BOOLEAN_VALUES =
[true, false, nil].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(complex_type, &block) ⇒ MapperBuilder

generated mappers fetch only the entries they need, so no filtering here



34
35
36
37
38
39
# File 'lib/odata_duty/mapper_builder.rb', line 34

def initialize(complex_type, &block)
  @complex_type = complex_type
  @set_types = complex_type.properties.to_h { |cp| [cp.name, cp.set_type] }
  @calling_methods = complex_type.properties.to_h { |cp| [cp.name, cp.calling_method] }
  @block = block
end

Instance Attribute Details

#calling_methodsObject (readonly)

Returns the value of attribute calling_methods.



31
32
33
# File 'lib/odata_duty/mapper_builder.rb', line 31

def calling_methods
  @calling_methods
end

#complex_typeObject (readonly)

Returns the value of attribute complex_type.



31
32
33
# File 'lib/odata_duty/mapper_builder.rb', line 31

def complex_type
  @complex_type
end

#contextObject

Returns the value of attribute context.



30
31
32
# File 'lib/odata_duty/mapper_builder.rb', line 30

def context
  @context
end

#objObject

Returns the value of attribute obj.



30
31
32
# File 'lib/odata_duty/mapper_builder.rb', line 30

def obj
  @obj
end

#set_typesObject (readonly)

Returns the value of attribute set_types.



31
32
33
# File 'lib/odata_duty/mapper_builder.rb', line 31

def set_types
  @set_types
end

Class Method Details

.build(complex_type, selected: nil) ⇒ Object



5
6
7
8
# File 'lib/odata_duty/mapper_builder.rb', line 5

def self.build(complex_type, selected: nil, &)
  mapper_class = build_class(complex_type, selected: selected)
  mapper_class.new(complex_type, &)
end

.build_class(complex_type, selected:) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/odata_duty/mapper_builder.rb', line 10

def self.build_class(complex_type, selected:)
  @dynamic_classes ||= {}
  @dynamic_classes[complex_type] ||= {}
  complex_classes = @dynamic_classes.fetch(complex_type)
  selected_key = selected&.map(&:name)&.sort
  complex_classes[selected_key] ||= eval_erb_class(complex_type, selected: selected)
end

.eval_erb_class(complex_type, selected:) ⇒ Object



23
24
25
26
27
28
# File 'lib/odata_duty/mapper_builder.rb', line 23

def self.eval_erb_class(complex_type, selected:)
  properties = selected || complex_type.properties
  class_result = ERB_TEMPLATE.result(binding)

  eval(class_result) # rubocop:disable Security/Eval
end

Instance Method Details

#confirm_boolean(name, value) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/odata_duty/mapper_builder.rb', line 64

def confirm_boolean(name, value)
  case value
  when true, false, nil
    value
  else
    raise InvalidValue, "Property #{name} must be a boolean and not #{value}"
  end
end

#confirm_one_of(name, value, valid_values) ⇒ Object

Raises:



79
80
81
82
83
84
# File 'lib/odata_duty/mapper_builder.rb', line 79

def confirm_one_of(name, value, valid_values)
  return nil if value.nil?
  return value if valid_values.include?(value)

  raise InvalidValue, "Property #{name} must be one of #{valid_values} and not #{value}"
end

#not_nullable(name, value) ⇒ Object

Raises:



73
74
75
76
77
# File 'lib/odata_duty/mapper_builder.rb', line 73

def not_nullable(name, value)
  raise InvalidValue, "Property #{name} cannot be null" if value.nil?

  value
end

#obj_to_base_hash(obj) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/odata_duty/mapper_builder.rb', line 54

def obj_to_base_hash(obj)
  return nil if obj.nil?

  @wrapped_obj = nil
  self.obj = obj
  to_h
end

#obj_to_hash(obj, context) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/odata_duty/mapper_builder.rb', line 45

def obj_to_hash(obj, context)
  @wrapped_obj = nil
  self.context = context
  self.obj = obj
  base_hash = to_h
  @block.call(base_hash, obj)
  base_hash
end

#wrapped_objObject



41
42
43
# File 'lib/odata_duty/mapper_builder.rb', line 41

def wrapped_obj
  @wrapped_obj ||= complex_type.new(obj, context)
end