Module: Appfuel::Domain::Dsl
- Included in:
- Entity
- Defined in:
- lib/appfuel/domain/dsl.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#equalizer ⇒ Object
readonly
Class macro dsl used to implement attributes.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
Instance Method Summary collapse
- #attribute(name, type_str, **options) ⇒ Object
- #attribute_conflict?(name, type) ⇒ Boolean
- #attribute_exists?(name, type) ⇒ Boolean
- #attribute_names ⇒ Object
- #build_type(type_str, **options) ⇒ Object
- #container_class_type ⇒ Object
- #create(inputs = {}) ⇒ Object (also: #call, #[])
- #default? ⇒ Boolean
- #disable_value_object ⇒ Object
- #domain_basename ⇒ Object
- #domain_name ⇒ Object
- #empty_hash(undefined_as_nil = false) ⇒ Object
- #enable_value_object ⇒ Object
- #enum(*args) ⇒ Object
- #inherited(klass) ⇒ Object
- #strict_enum(*args) ⇒ Object
- #try(input) ⇒ Object
- #type(str) ⇒ Object
- #valid?(value) ⇒ Boolean
- #value_object? ⇒ Boolean
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
15 16 17 |
# File 'lib/appfuel/domain/dsl.rb', line 15 def defaults @defaults end |
#equalizer ⇒ Object
Class macro dsl used to implement attributes
14 15 16 |
# File 'lib/appfuel/domain/dsl.rb', line 14 def equalizer @equalizer end |
#schema ⇒ Object (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, **) unless type_str.is_a?(String) return handle_manual_type(name, type_str, ) end name = name.to_sym type = build_type(type_str, ) schema[name] = type unless attribute_exists?(name, type) nil end |
#attribute_conflict?(name, type) ⇒ 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
106 107 108 |
# File 'lib/appfuel/domain/dsl.rb', line 106 def attribute_exists?(name, type) schema[name.to_sym] === type end |
#attribute_names ⇒ Object
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, **) base = type_str.split('.').last type = Types[type_str] type = apply_optional(type, ) nil_is_allowed = allow_nil?() type = case base when 'hash' then handle_hash(type, ) when 'array' then handle_array(type, ) else type end type = apply_defaults(type, ) type = apply_constraints(type, ) # You have to apply all the contraints before summing nil type = sum_nil(type) if nil_is_allowed type end |
#container_class_type ⇒ Object
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
38 39 40 |
# File 'lib/appfuel/domain/dsl.rb', line 38 def default? false end |
#disable_value_object ⇒ Object
54 55 56 |
# File 'lib/appfuel/domain/dsl.rb', line 54 def disable_value_object @value_object = false end |
#domain_basename ⇒ Object
133 134 135 |
# File 'lib/appfuel/domain/dsl.rb', line 133 def domain_basename domain_name.split('.').last end |
#domain_name ⇒ Object
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_object ⇒ Object
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.) 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
42 43 44 |
# File 'lib/appfuel/domain/dsl.rb', line 42 def valid?(value) self === value end |
#value_object? ⇒ Boolean
46 47 48 |
# File 'lib/appfuel/domain/dsl.rb', line 46 def value_object? @value_object end |