Class: Methodist::Builder
Class Method Summary collapse
- .attr_accessor(*args) ⇒ Object
-
.attributes ⇒ Object
(also: attrs)
Array of defined attributes.
-
.field(attr_name, proc = nil) ⇒ 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
- .set_proc_to_const(proc, 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
41 42 43 |
# File 'lib/methodist/builder.rb', line 41 def attributes @attributes || [] end |
.field(attr_name, proc = nil) ⇒ 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 |
# File 'lib/methodist/builder.rb', line 21 def field(attr_name, proc = nil) attr_accessor(attr_name) set_proc_to_const(proc, attr_name) if 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.
34 35 36 |
# File 'lib/methodist/builder.rb', line 34 def fields(*attr_names, &block) attr_names.each { |attr_name| field(attr_name, &block) } end |
.proc_const_name(attr_name) ⇒ Object
51 52 53 |
# File 'lib/methodist/builder.rb', line 51 def proc_const_name(attr_name) "VALIDATION_PROC_#{attr_name.upcase}" end |
.set_proc_to_const(proc, attr_name) ⇒ Object
47 48 49 |
# File 'lib/methodist/builder.rb', line 47 def set_proc_to_const(proc, attr_name) const_set(proc_const_name(attr_name), proc) 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' }
64 65 66 67 68 69 70 |
# File 'lib/methodist/builder.rb', line 64 def to_h hash = {} self.class.attributes.each do |att| hash[att] = self.send(att) end hash end |
#valid? ⇒ Boolean
Validate all attributes.
77 78 79 |
# File 'lib/methodist/builder.rb', line 77 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
89 90 91 92 93 |
# File 'lib/methodist/builder.rb', line 89 def valid_attr?(attr_name) proc = get_proc(attr_name) return true if proc.nil? proc.call(self.send(attr_name)) end |