Class: Methodist::Builder
Defined Under Namespace
Classes: InvalidValueError
Class Method Summary collapse
- .attr_accessor(*args) ⇒ Object
-
.attributes ⇒ Object
(also: attrs)
Array of defined attributes.
-
.field(attr_name, proc = nil, **options) ⇒ Object
Define attribute and set validation for value.
-
.fields(*attr_names, &block) ⇒ Object
Define multiple attributes and set validation for values.
- .proc_const_name(attr_name) ⇒ Object
Instance Method Summary collapse
-
#to_h ⇒ Object
(also: #to_hash)
Convert attributes and values to hash.
-
#valid? ⇒ Boolean
Validate all attributes.
-
#valid_attr?(attr_name) ⇒ Boolean
Validate attribute value.
Methods inherited from Pattern
Class Method Details
.attr_accessor(*args) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/methodist/builder.rb', line 5 def attr_accessor(*args) @attributes ||= [] @attributes += args @attributes.uniq! super(*args) end |
.attributes ⇒ Object Also known as: attrs
Array of defined attributes
42 43 44 |
# File 'lib/methodist/builder.rb', line 42 def attributes @attributes || [] end |
.field(attr_name, proc = nil, **options) ⇒ Object
Define attribute and set validation for value
Parameters
-
attr_name
[String] - name of defined attribute -
proc
[Proc] - proc for validate contains value. If proc call result
will returns true then #valid_attr? will returns true.
21 22 23 24 25 |
# File 'lib/methodist/builder.rb', line 21 def field(attr_name, proc = nil, **) attr_accessor(attr_name) define_read_method_with_default(attr_name, [:default]) define_write_method(attr_name, proc, ) end |
.fields(*attr_names, &block) ⇒ Object
Define multiple attributes and set validation for values
Parameters
-
attr_names
[Args] - name of defined attribute -
proc
[Proc] - proc for validate contains values.
35 36 37 |
# File 'lib/methodist/builder.rb', line 35 def fields(*attr_names, &block) attr_names.each { |attr_name| field(attr_name, &block) } end |
.proc_const_name(attr_name) ⇒ Object
48 49 50 |
# File 'lib/methodist/builder.rb', line 48 def proc_const_name(attr_name) "VALIDATION_PROC_#{attr_name.upcase}" end |
Instance Method Details
#to_h ⇒ Object Also known as: to_hash
Convert attributes and values to hash.
Example
In builder with attributes 'title', 'author'
when you will use this method you will have:
{ title: 'Title', author: 'Author' }
89 90 91 92 93 94 95 |
# File 'lib/methodist/builder.rb', line 89 def to_h hash = {} self.class.attributes.each do |att| hash[att] = self.send(att) end hash end |
#valid? ⇒ Boolean
Validate all attributes.
102 103 104 |
# File 'lib/methodist/builder.rb', line 102 def valid? self.class.attributes.all? { |att| valid_attr?(att) } end |
#valid_attr?(attr_name) ⇒ Boolean
Validate attribute value. Proc passed in #field method use for validation. If proc was not defined just return true.
Parameters
-
attr_name
[String/Symbol] - name of attribute
114 115 116 117 118 |
# File 'lib/methodist/builder.rb', line 114 def valid_attr?(attr_name) proc = get_proc(attr_name) return true if proc.nil? proc.call(self.send(attr_name)) end |