Module: Appfuel::Domain::Dsl

Included in:
Entity
Defined in:
lib/appfuel/domain/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



15
16
17
# File 'lib/appfuel/domain/dsl.rb', line 15

def defaults
  @defaults
end

#equalizerObject

Class macro dsl used to implement attributes



14
15
16
# File 'lib/appfuel/domain/dsl.rb', line 14

def equalizer
  @equalizer
end

#schemaObject (readonly)

Returns the value of attribute schema.



15
16
17
# File 'lib/appfuel/domain/dsl.rb', line 15

def schema
  @schema
end

Class Method Details

.extended(base) ⇒ Object



18
19
20
21
# File 'lib/appfuel/domain/dsl.rb', line 18

def self.extended(base)
  base.instance_variable_set(:@schema, {})
  base.instance_variable_set(:@value_object, false)
end

Instance Method Details

#attribute(name, type_str, **options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/appfuel/domain/dsl.rb', line 74

def attribute(name, type_str, **options)
  unless type_str.is_a?(String)
    return handle_manual_type(name, type_str, options)
  end

  name = name.to_sym
  type = build_type(type_str, options)
  schema[name] = type unless attribute_exists?(name, type)
  nil
end

#attribute_conflict?(name, type) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/appfuel/domain/dsl.rb', line 110

def attribute_conflict?(name, type)
  name = name.to_sym
  schema.key?(name) && schema[name] != type
end

#attribute_exists?(name, type) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/appfuel/domain/dsl.rb', line 106

def attribute_exists?(name, type)
  schema[name.to_sym] === type
end

#attribute_namesObject



70
71
72
# File 'lib/appfuel/domain/dsl.rb', line 70

def attribute_names
  schema.keys
end

#build_type(type_str, **options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/appfuel/domain/dsl.rb', line 85

def build_type(type_str, **options)
  base = type_str.split('.').last
  type = Types[type_str]
  type = apply_optional(type, options)
  nil_is_allowed = allow_nil?(options)
  type = case base
         when 'hash'  then handle_hash(type, options)
         when 'array' then handle_array(type, options)
         else
           type
         end

  type = apply_defaults(type, options)
  type = apply_constraints(type, options)

  # You have to apply all the contraints before summing nil
  type = sum_nil(type) if nil_is_allowed

  type
end

#container_class_typeObject



34
35
36
# File 'lib/appfuel/domain/dsl.rb', line 34

def container_class_type
  'domains'
end

#create(inputs = {}) ⇒ Object Also known as: call, []



115
116
117
# File 'lib/appfuel/domain/dsl.rb', line 115

def create(inputs = {})
  self.new(inputs)
end

#default?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/appfuel/domain/dsl.rb', line 38

def default?
  false
end

#disable_value_objectObject



54
55
56
# File 'lib/appfuel/domain/dsl.rb', line 54

def disable_value_object
  @value_object = false
end

#domain_basenameObject



133
134
135
# File 'lib/appfuel/domain/dsl.rb', line 133

def domain_basename
  domain_name.split('.').last
end

#domain_nameObject



129
130
131
# File 'lib/appfuel/domain/dsl.rb', line 129

def domain_name
  @domain_name ||= build_domain_name
end

#empty_hash(undefined_as_nil = false) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/appfuel/domain/dsl.rb', line 137

def empty_hash(undefined_as_nil = false)
  data  = {}
  value = undefined_as_nil == true ? nil : Types::Undefined
  schema.keys.each do |key|
    data[key] = value
  end
  data
end

#enable_value_objectObject



50
51
52
# File 'lib/appfuel/domain/dsl.rb', line 50

def enable_value_object
  @value_object = true
end

#enum(*args) ⇒ Object



66
67
68
# File 'lib/appfuel/domain/dsl.rb', line 66

def enum(*args)
  type('coercible.string').enum(*args)
end

#inherited(klass) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/appfuel/domain/dsl.rb', line 23

def inherited(klass)
  super
  klass.instance_variable_set(:@value_object, false)
  klass.instance_variable_set(:@schema, {})
  klass.equalizer = Dry::Equalizer.new(*schema.keys)
  klass.send(:include, klass.equalizer)

  stage_class_for_registration(klass)
  Types.register_domain(klass)
end

#strict_enum(*args) ⇒ Object



62
63
64
# File 'lib/appfuel/domain/dsl.rb', line 62

def strict_enum(*args)
  type('strict.string').enum(*args)
end

#try(input) ⇒ Object



122
123
124
125
126
127
# File 'lib/appfuel/domain/dsl.rb', line 122

def try(input)
  Dry::Types::Result::Success.new(self[input])
rescue => e
  failure = Dry::Types::Result::Failure.new(input, e.message)
  block_given? ? yield(failure) : failure
end

#type(str) ⇒ Object



58
59
60
# File 'lib/appfuel/domain/dsl.rb', line 58

def type(str)
  Types[str]
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/appfuel/domain/dsl.rb', line 42

def valid?(value)
  self === value
end

#value_object?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/appfuel/domain/dsl.rb', line 46

def value_object?
  @value_object
end