Class: Pocolog::Upgrade::DSL::Context

Inherits:
BasicObject
Defined in:
lib/pocolog/upgrade/dsl.rb

Overview

Object providing an evaluation context for the converter DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, converter_registry, source_registry, target_registry) ⇒ Context

Returns a new instance of Context



15
16
17
18
19
20
21
22
# File 'lib/pocolog/upgrade/dsl.rb', line 15

def initialize(path, converter_registry, source_registry, target_registry)
    @path = path
    @converter_registry = converter_registry
    @source_registry = source_registry
    @target_registry = target_registry
    @deep_cast = nil
    @reference_date = nil
end

Instance Attribute Details

#converterObject (readonly)

Returns the value of attribute converter



13
14
15
# File 'lib/pocolog/upgrade/dsl.rb', line 13

def converter
  @converter
end

#converter_registryObject (readonly)

Returns the value of attribute converter_registry



12
13
14
# File 'lib/pocolog/upgrade/dsl.rb', line 12

def converter_registry
  @converter_registry
end

#from_typeObject (readonly)

Returns the value of attribute from_type



8
9
10
# File 'lib/pocolog/upgrade/dsl.rb', line 8

def from_type
  @from_type
end

#pathObject (readonly)

Returns the value of attribute path



6
7
8
# File 'lib/pocolog/upgrade/dsl.rb', line 6

def path
  @path
end

#reference_dateObject (readonly)

Returns the value of attribute reference_date



7
8
9
# File 'lib/pocolog/upgrade/dsl.rb', line 7

def reference_date
  @reference_date
end

#source_registryObject (readonly)

Returns the value of attribute source_registry



10
11
12
# File 'lib/pocolog/upgrade/dsl.rb', line 10

def source_registry
  @source_registry
end

#target_registryObject (readonly)

Returns the value of attribute target_registry



11
12
13
# File 'lib/pocolog/upgrade/dsl.rb', line 11

def target_registry
  @target_registry
end

#to_typeObject (readonly)

Returns the value of attribute to_type



9
10
11
# File 'lib/pocolog/upgrade/dsl.rb', line 9

def to_type
  @to_type
end

Instance Method Details

#copy(target, value) ⇒ Object



42
43
44
# File 'lib/pocolog/upgrade/dsl.rb', line 42

def copy(target, value)
    ::Typelib.copy(target, ::Typelib.from_ruby(value, target.class))
end

#deep_cast(target, value, relax: true, **options) ⇒ Object



35
36
37
38
39
40
# File 'lib/pocolog/upgrade/dsl.rb', line 35

def deep_cast(target, value, relax: true, **options)
    if !@deep_cast_ops
        @deep_cast_ops = ::Pocolog::Upgrade.build_deep_cast(reference_date, from_type, to_type, converter_registry, relax: relax, **options)
    end
    @deep_cast_ops.call(target, value)
end

#define(date_string, from_type_name, to_type_name, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/pocolog/upgrade/dsl.rb', line 24

def define(date_string, from_type_name, to_type_name, &block)
    if @reference_date
        Kernel.raise ::ArgumentError, "one can define only one converter per file"
    end

    @reference_date = ::DateTime.parse(date_string).to_time
    @from_type = source_registry.get(from_type_name)
    @to_type = target_registry.get(to_type_name)
    @converter = converter_registry.add(reference_date, from_type, to_type, name: path.to_s, &block)
end

#to_ruby(value) ⇒ Object



46
47
48
# File 'lib/pocolog/upgrade/dsl.rb', line 46

def to_ruby(value)
    ::Typelib.to_ruby(value)
end