Module: LunaPark::Extensions::Dsl::Attributes
- Includes:
- PredicateAttrAccessor, TypedAttrAccessor
- Included in:
- LunaPark::Entities::Attributable, Values::Attributable
- Defined in:
- lib/luna_park/extensions/dsl/attributes.rb
Overview
class-level mixin
Instance Method Summary collapse
-
#attr(name, type = nil, type_meth = nil, comparable: true, array: false) ⇒ Hash(Symbol => Symbol)
define typed attr_accessor, register it for Extenions::Comparable, Extenions::Serializable so it will be comparable using ‘#==`, `#eql?` and serializable using `#to_h`, `#serialize` return Hash of defined methods `{ getter: :foo, setter: :foo= }`.
-
#attr?(*args, **options) ⇒ Hash(Symbol => Symbol)
.attrs that also adds ‘reader?` return Hash of defined methods `{ getter: :foo, setter: :foo=, predicate }`.
-
#attrs(*args, **options) ⇒ Array of Hash(Symbol => Symbol)
.attr with mass defining.
-
#attrs?(*args, **options) ⇒ Array of Hash(Symbol => Symbol)
.attr that also adds ‘reader?`.
Methods included from TypedAttrAccessor
#typed_attr_accessor, #typed_attr_writer
Methods included from PredicateAttrAccessor
#predicate_attr_accessor, #predicate_attr_reader
Instance Method Details
#attr(name, type = nil, type_meth = nil, comparable: true, array: false) ⇒ Hash(Symbol => Symbol)
define typed attr_accessor, register it for Extenions::Comparable, Extenions::Serializable
so it will be comparable using `#==`, `#eql?` and serializable using `#to_h`, `#serialize`
return Hash of defined methods `{ getter: :foo, setter: :foo= }`
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/luna_park/extensions/dsl/attributes.rb', line 121 def attr(name, type = nil, type_meth = nil, comparable: true, array: false) type_meth ||= DEFAULT_TYPE_METH attr_reader(name) serializable_attributes(name) if include?(Serializable) comparable_attributes(name) if comparable && include?(Comparable) typed_attr_writer(name, type&.method(type_meth), is_array: array) { getter: name, setter: :"#{name}=" } end |
#attr?(*args, **options) ⇒ Hash(Symbol => Symbol)
.attrs that also adds ‘reader?`
return Hash of defined methods `{ getter: :foo, setter: :foo=, predicate }`
73 74 75 76 77 78 79 80 81 |
# File 'lib/luna_park/extensions/dsl/attributes.rb', line 73 def attr?(*args, **) defined_methods = attr(*args, **) getter_name = defined_methods[:getter] protected(getter_name) attr_reader?(getter_name) defined_methods.merge(predicate: :"#{getter_name}?") end |
#attrs(*args, **options) ⇒ Array of Hash(Symbol => Symbol)
.attr with mass defining
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/luna_park/extensions/dsl/attributes.rb', line 92 def attrs(*args, **) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity *names, type, type_meth = if args.all? { |arg| arg.is_a?(Symbol) } [*args, nil, nil] elsif args[0..-2].all? { |arg| arg.is_a?(Symbol) } [*args, DEFAULT_TYPE_METH] elsif args[0..-3].all? { |arg| arg.is_a?(Symbol) } && args.last.is_a?(Symbol) args else raise ArgumentError, 'must be (*names) | ' \ '(*names, type) | (*names, type, type_meth)' end names.map { |name| attr name, type, type_meth, ** } end |
#attrs?(*args, **options) ⇒ Array of Hash(Symbol => Symbol)
.attr that also adds ‘reader?`
58 59 60 61 62 63 64 65 66 |
# File 'lib/luna_park/extensions/dsl/attributes.rb', line 58 def attrs?(*args, **) defined_methods_arr = attrs(*args, **) getter_names = defined_methods_arr.map { |r| r[:getter] } protected(*getter_names) attr_reader?(*getter_names) defined_methods_arr.map { |r| r.merge(predicate: :"#{r[:getter]}?") } end |