Module: Plumb::Attributes::ClassMethods
- Defined in:
- lib/plumb/attributes.rb
Instance Method Summary collapse
-
#[](type_specs) ⇒ Object
Person = Data[:name => String, :age => Integer, title?: String].
- #__plumb_define_attribute_reader_method__(name) ⇒ Object
- #__plumb_define_attribute_writer_method__(name) ⇒ Object
- #__set_nested_class__(name, klass) ⇒ Object
- #_schema ⇒ Object
-
#attribute(name, type = Types::Any, writer: false, &block) ⇒ Object
attribute(:friend) { attribute(:name, String) } attribute(:friend, MyStruct) { attribute(:name, String) } attribute(:name, String) attribute(:friends, Types::Array) { attribute(:name, String) } attribute(:friends, Types::Array) # same as :Array attribute(:friends, []) # same as :Array attribute(:friends, Types::Array) attribute(:friends, [Person]).
- #attribute?(name, *args, &block) ⇒ Boolean
- #build_nested(name, node, &block) ⇒ Object
-
#call(result) ⇒ Plumb::Result::Valid, Plumb::Result::Invalid
The Plumb::Step interface.
- #inherited(subclass) ⇒ Object
-
#node_name ⇒ Object
node name for visitors.
Instance Method Details
#[](type_specs) ⇒ Object
Person = Data[:name => String, :age => Integer, title?: String]
199 200 201 202 203 204 205 206 |
# File 'lib/plumb/attributes.rb', line 199 def [](type_specs) type_specs = type_specs._schema if type_specs.is_a?(Plumb::HashClass) klass = Class.new(self) type_specs.each do |key, type| klass.attribute(key, type) end klass end |
#__plumb_define_attribute_reader_method__(name) ⇒ Object
249 250 251 |
# File 'lib/plumb/attributes.rb', line 249 def __plumb_define_attribute_reader_method__(name) define_method(name) { @attributes[name] } end |
#__plumb_define_attribute_writer_method__(name) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/plumb/attributes.rb', line 253 def __plumb_define_attribute_writer_method__(name) define_method("#{name}=") do |value| type = self.class._schema.at_key(name) result = type.resolve(value) @attributes[name] = result.value if result.valid? @errors.delete(name) else @errors.merge!(name => result.errors) end result.value end end |
#__set_nested_class__(name, klass) ⇒ Object
290 291 292 293 |
# File 'lib/plumb/attributes.rb', line 290 def __set_nested_class__(name, klass) name = name.to_s.split('_').map(&:capitalize).join.sub(/s$/, '') const_set(name, klass) unless const_defined?(name) end |
#_schema ⇒ Object
176 177 178 |
# File 'lib/plumb/attributes.rb', line 176 def _schema @_schema ||= HashClass.new end |
#attribute(name, type = Types::Any, writer: false, &block) ⇒ Object
attribute(:friend) { attribute(:name, String) } attribute(:friend, MyStruct) { attribute(:name, String) } attribute(:name, String) attribute(:friends, Types::Array) { attribute(:name, String) } attribute(:friends, Types::Array) # same as :Array attribute(:friends, []) # same as :Array attribute(:friends, Types::Array) attribute(:friends, [Person])
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/plumb/attributes.rb', line 220 def attribute(name, type = Types::Any, writer: false, &block) key = Key.wrap(name) name = key.to_sym type = Composable.wrap(type) if block_given? # :foo, Array[Data] or :foo, Struct type = __plumb_struct_class__ if type == Types::Any type = Plumb.decorate(type) do |node| if node.is_a?(Plumb::ArrayClass) child = node.children.first child = __plumb_struct_class__ if child == Types::Any Types::Array[build_nested(name, child, &block)] elsif node.is_a?(Plumb::Step) build_nested(name, node, &block) elsif node.is_a?(Class) && node <= Plumb::Attributes build_nested(name, node, &block) else node end end end @_schema = _schema + { key => type } __plumb_define_attribute_reader_method__(name) return name unless writer __plumb_define_attribute_writer_method__(name) end |
#attribute?(name, *args, &block) ⇒ Boolean
267 268 269 |
# File 'lib/plumb/attributes.rb', line 267 def attribute?(name, *args, &block) attribute(Key.new(name, optional: true), *args, &block) end |
#build_nested(name, node, &block) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/plumb/attributes.rb', line 271 def build_nested(name, node, &block) if node.is_a?(Class) && node <= Plumb::Attributes sub = Class.new(node) sub.instance_exec(&block) __set_nested_class__(name, sub) return Composable.wrap(sub) end return node unless node.is_a?(Plumb::Step) child = node.children.first return node unless child <= Plumb::Attributes sub = Class.new(child) sub.instance_exec(&block) __set_nested_class__(name, sub) Composable.wrap(sub) end |
#call(result) ⇒ Plumb::Result::Valid, Plumb::Result::Invalid
The Plumb::Step interface
190 191 192 193 194 195 196 |
# File 'lib/plumb/attributes.rb', line 190 def call(result) return result if result.value.is_a?(self) return result.invalid(errors: ['Must be a Hash of attributes']) unless result.value.respond_to?(:to_h) instance = new(result.value.to_h) instance.valid? ? result.valid(instance) : result.invalid(instance, errors: instance.errors.to_h) end |
#inherited(subclass) ⇒ Object
180 181 182 183 184 185 |
# File 'lib/plumb/attributes.rb', line 180 def inherited(subclass) _schema._schema.each do |key, type| subclass.attribute(key, type) end super end |
#node_name ⇒ Object
node name for visitors
209 |
# File 'lib/plumb/attributes.rb', line 209 def node_name = :data |