Class: Dialekt::Model::BasicProperty

Inherits:
Object
  • Object
show all
Defined in:
lib/dialekt/model/basic_property.rb

Overview

BaseAccessor base class

Direct Known Subclasses

MapProperty, ScalarProperty, SetProperty

Defined Under Namespace

Classes: Shape

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type: nil, factory: nil, transformer: nil) ⇒ BasicProperty

Returns a new instance of BasicProperty.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
# File 'lib/dialekt/model/basic_property.rb', line 22

def initialize(name:, type: nil, factory: nil, transformer: nil)
  raise ArgumentError, "Name must not be nil" if name.nil?

  @name = name
  @type = type

  @variable = :"@#{name}"
  @factory = factory&.call_adapter
  @transformer = transformer&.call_adapter
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/dialekt/model/basic_property.rb', line 19

def name
  @name
end

#type(type = EMPTY) ⇒ Object



74
75
76
# File 'lib/dialekt/model/basic_property.rb', line 74

def type(type = EMPTY)
  type == EMPTY ? @type : (@type = type)
end

Instance Method Details

#access_value(shape:, target:, value: EMPTY) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/dialekt/model/basic_property.rb', line 35

def access_value(shape:, target:, value: EMPTY)
  if value.equal?(EMPTY)
    get_value(shape: shape, target: target)
  else
    set_value(shape: shape, target: target, value: value)
  end
end

#factory(factory = EMPTY, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dialekt/model/basic_property.rb', line 78

def factory(factory = EMPTY, &block)
  if factory == EMPTY
    if block
      self.factory = block
    else
      @factory
    end
  else
    raise ArgumentError, "Please provide either a factory proc or a block, not both" if block

    self.factory = factory
  end
end

#factory=(factory) ⇒ Object



92
93
94
# File 'lib/dialekt/model/basic_property.rb', line 92

def factory=(factory)
  @factory = factory&.call_adapter
end

#get_value(shape:, target:) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/dialekt/model/basic_property.rb', line 43

def get_value(shape:, target:)
  if target.instance_variable_defined?(@variable)
    target.instance_variable_get(@variable)
  else
    value = shape.factory&.call(object: target)
    target.instance_variable_set(@variable, value)
  end
end

#set_value(shape:, target:, value:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dialekt/model/basic_property.rb', line 52

def set_value(shape:, target:, value:)
  if shape.transformer
    begin
      value = shape.transformer.call(object: target, value: value)
    rescue StandardError
      raise TypeError, "Cannot transform value '#{value}' for property #{@name}"
    end
  end

  type_checker = target.class.dialekt_type_checker

  begin
    type_checker.check!(type: shape.type, value: value)
  rescue StandardError
    raise TypeError, <<~MSG
      Value '#{value}' (#{value.class}) for property #{@name} must conform to #{type_checker.format(type: shape.type)}
    MSG
  end

  target.instance_variable_set(@variable, value)
end

#setup(owner:) ⇒ Object



33
# File 'lib/dialekt/model/basic_property.rb', line 33

def setup(owner:); end

#transformer(transformer = EMPTY, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dialekt/model/basic_property.rb', line 96

def transformer(transformer = EMPTY, &block)
  if transformer == EMPTY
    if block
      self.transformer = block
    else
      @transformer
    end
  else
    raise ArgumentError, "Please provide either a transformer proc or a block, not both" if block

    self.transformer = transformer
  end
end

#transformer=(transformer) ⇒ Object



110
111
112
# File 'lib/dialekt/model/basic_property.rb', line 110

def transformer=(transformer)
  @transformer = transformer&.call_adapter
end