Module: Plumb::Attributes
- Included in:
- Types::Data
- Defined in:
- lib/plumb/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
-
.included(base) ⇒ Object
A module that provides a simple way to define a struct-like class with attributes that are type-checked on initialization.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #deconstruct ⇒ Object
- #deconstruct_keys ⇒ Object
- #initialize(attrs = {}) ⇒ Object
- #inspect ⇒ Object
- #to_h ⇒ Hash
- #valid? ⇒ Boolean
- #with(attrs = BLANK_HASH) ⇒ Plumb::Attributes
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
116 117 118 |
# File 'lib/plumb/attributes.rb', line 116 def attributes @attributes end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
116 117 118 |
# File 'lib/plumb/attributes.rb', line 116 def errors @errors end |
Class Method Details
.included(base) ⇒ Object
A module that provides a simple way to define a struct-like class with attributes that are type-checked on initialization.
It supports nested attributes:
Or arrays of nested attributes:
Or use struct classes defined separately:
Arrays and other types support composition and helpers. Ex. ‘#default`.
attribute :companies, Types::Array[Company].default([].freeze)
Passing a named struct class AND a block will subclass the struct and extend it with new attributes:
attribute :company, Company do
attribute :address, String
end
The same works with arrays:
attribute :companies, Types::Array[Company] do
attribute :address, String
end
Note that this does NOT work with union’d or piped structs.
attribute :company, Company | Person do
## Optional Attributes Using ‘attribute?` allows for optional attributes. If the attribute is not present, it will be set to `Undefined`.
attribute? :company, Company
## Struct Inheritance Structs can inherit from other structs. This is useful for defining a base struct with common attributes.
class BasePerson
include Plumb::Attributes
attribute :name, String
end
class Person < BasePerson
attribute :age, Integer
end
## [] Syntax
The ‘[]` syntax can be used to define a struct in a single line. Like Plumb::Types::Hash, suffixing a key with `?` makes it optional.
Person = Data[name: String, age?: Integer]
person = Person.new(name: 'Jane')
111 112 113 114 |
# File 'lib/plumb/attributes.rb', line 111 def self.included(base) base.send(:extend, ClassMethods) base.define_singleton_method(:__plumb_struct_class__) { base } end |
Instance Method Details
#==(other) ⇒ Object
123 124 125 |
# File 'lib/plumb/attributes.rb', line 123 def ==(other) other.is_a?(self.class) && other.attributes == attributes end |
#deconstruct ⇒ Object
159 |
# File 'lib/plumb/attributes.rb', line 159 def deconstruct(...) = to_h.values.deconstruct(...) |
#deconstruct_keys ⇒ Object
160 |
# File 'lib/plumb/attributes.rb', line 160 def deconstruct_keys(...) = to_h.deconstruct_keys(...) |
#initialize(attrs = {}) ⇒ Object
118 119 120 121 |
# File 'lib/plumb/attributes.rb', line 118 def initialize(attrs = {}) assign_attributes(attrs) freeze end |
#inspect ⇒ Object
136 137 138 139 140 |
# File 'lib/plumb/attributes.rb', line 136 def inspect %(#<#{self.class}:#{object_id} [#{valid? ? 'valid' : 'invalid'}] #{attributes.map do |k, v| [k, v.inspect].join(':') end.join(' ')}>) end |
#to_h ⇒ Hash
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/plumb/attributes.rb', line 143 def to_h self.class._schema._schema.keys.each.with_object({}) do |key, memo| key = key.to_sym value = attributes[key] val = case value when ::Array value.map { |v| v.respond_to?(:to_h) ? v.to_h : v } when ::NilClass nil else value.respond_to?(:to_h) ? value.to_h : value end memo[key] = val end end |
#valid? ⇒ Boolean
128 |
# File 'lib/plumb/attributes.rb', line 128 def valid? = !errors || errors.none? |
#with(attrs = BLANK_HASH) ⇒ Plumb::Attributes
132 133 134 |
# File 'lib/plumb/attributes.rb', line 132 def with(attrs = BLANK_HASH) self.class.new(attributes.merge(attrs)) end |