Class: Dialekt::Model::ScalarProperty
Overview
Base class for primitive DSL properties
Instance Attribute Summary
#name, #type
Instance Method Summary
collapse
#access_value, #factory, #factory=, #get_value, #set_value, #transformer, #transformer=
Constructor Details
#initialize(name:, type: nil, factory: nil, transformer: nil) ⇒ ScalarProperty
Returns a new instance of ScalarProperty.
9
10
11
12
13
|
# File 'lib/dialekt/model/scalar_property.rb', line 9
def initialize(name:, type: nil, factory: nil, transformer: nil)
super(name: name, type: type, factory: factory, transformer: transformer)
@shapes = {}
end
|
Instance Method Details
#setup(owner:) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/dialekt/model/scalar_property.rb', line 15
def setup(owner:)
super
raise ArgumentError, "Missing type for property #{name} of #{owner}" if @shapes.empty? && @type.nil?
unless @shapes.key?(name)
@shapes[name] = BasicProperty::Shape.new(
name: name,
type: @type || owner.class.type_checker.union_type(types: @shapes.values.map(&:type)),
factory: @factory,
transformer: @transformer
)
end
property = self
@shapes.each_value do |shape|
owner.define_method(shape.name) do |value = EMPTY, &block|
property.access_value(shape: shape, target: self, value: value, &block)
end
owner.define_method(:"#{shape.name}=") do |value|
property.set_value(shape: shape, target: self, value: value)
end
end
end
|
#shape(name = nil, **options) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/dialekt/model/scalar_property.rb', line 42
def shape(name = nil, **options)
name = name&.to_sym || self.name
raise ArgumentError, "Property #{self.name} already has a shape called #{name}" if @shapes.key?(name)
options[:type] ||= @type
options[:factory] ||= @factory
options[:transformer] ||= @transformer
raise ArgumentError, "Missing shape for value #{name} of property #{self.name}" if options[:type].nil?
config = BasicProperty::Shape.new(name: name, **options)
@shapes[name] = config
end
|
#shapes ⇒ Object
57
58
59
|
# File 'lib/dialekt/model/scalar_property.rb', line 57
def shapes
@shapes.dup.freeze
end
|
#shapes=(shapes) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/dialekt/model/scalar_property.rb', line 61
def shapes=(shapes)
shapes = shapes.values if shapes.is_a?(Hash)
raise ArgumentError, "Shapes must be an Enumerable" unless shapes.is_a?(Enumerable)
@shapes = shapes.map { |shape| [shape.name, shape] }.to_h
end
|