Class: Structure::Builder
- Inherits:
-
Object
- Object
- Structure::Builder
- Defined in:
- lib/structure/builder.rb
Overview
Builder class for accumulating attribute definitions
Instance Attribute Summary collapse
- #after_parse_callback ⇒ Object readonly private
- #defaults ⇒ Object readonly private
- #mappings ⇒ Object readonly private
- #types ⇒ Object readonly private
Instance Method Summary collapse
-
#after_parse {|instance| ... } ⇒ void
Defines a callback to run after parsing.
-
#attribute(name, type = nil, from: nil, default: nil) {|value| ... } ⇒ Object
DSL method for defining attributes with optional type coercion.
-
#attribute?(name, type = nil, from: nil, default: nil) {|value| ... } ⇒ Boolean
DSL method for defining optional attributes (key can be missing from input hash).
- #attributes ⇒ Object private
- #coercions(context = nil) ⇒ Object private
-
#initialize ⇒ Builder
constructor
private
A new instance of Builder.
- #optional ⇒ Object private
- #predicate_methods ⇒ Object private
- #required ⇒ Object private
Constructor Details
#initialize ⇒ Builder
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Builder.
12 13 14 15 16 17 |
# File 'lib/structure/builder.rb', line 12 def initialize @mappings = {} @defaults = {} @types = {} @optional = Set.new end |
Instance Attribute Details
#after_parse_callback ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
9 10 11 |
# File 'lib/structure/builder.rb', line 9 def after_parse_callback @after_parse_callback end |
#defaults ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
9 10 11 |
# File 'lib/structure/builder.rb', line 9 def defaults @defaults end |
#mappings ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
9 10 11 |
# File 'lib/structure/builder.rb', line 9 def mappings @mappings end |
#types ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
9 10 11 |
# File 'lib/structure/builder.rb', line 9 def types @types end |
Instance Method Details
#after_parse {|instance| ... } ⇒ void
This method returns an undefined value.
Defines a callback to run after parsing
77 78 79 |
# File 'lib/structure/builder.rb', line 77 def after_parse(&block) @after_parse_callback = block end |
#attribute(name, type = nil, from: nil, default: nil) {|value| ... } ⇒ Object
DSL method for defining attributes with optional type coercion
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/structure/builder.rb', line 38 def attribute(name, type = nil, from: nil, default: nil, &block) mappings[name] = from || name.to_s defaults[name] = default unless default.nil? if type && block raise ArgumentError, "Cannot specify both type and block for :#{name}" else types[name] = type || block end end |
#attribute?(name, type = nil, from: nil, default: nil) {|value| ... } ⇒ Boolean
DSL method for defining optional attributes (key can be missing from input hash)
63 64 65 66 |
# File 'lib/structure/builder.rb', line 63 def attribute?(name, type = nil, from: nil, default: nil, &block) attribute(name, type, from: from, default: default, &block) @optional.add(name) end |
#attributes ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
82 |
# File 'lib/structure/builder.rb', line 82 def attributes = @mappings.keys |
#coercions(context = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
91 92 93 |
# File 'lib/structure/builder.rb', line 91 def coercions(context = nil) @types.transform_values { |type| Types.coerce(type, context) } end |
#optional ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
85 |
# File 'lib/structure/builder.rb', line 85 def optional = @optional.to_a |
#predicate_methods ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 99 100 101 102 |
# File 'lib/structure/builder.rb', line 96 def predicate_methods @types.filter_map do |name, type| if type == :boolean ["#{name}?".to_sym, name] unless name.to_s.end_with?("?") end end.to_h end |
#required ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
88 |
# File 'lib/structure/builder.rb', line 88 def required = attributes - optional |