Class: Simple::Printer::Field
- Inherits:
-
Object
- Object
- Simple::Printer::Field
- Defined in:
- lib/simple-printer.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#key ⇒ Object
Returns the value of attribute key.
-
#label ⇒ Object
Returns the value of attribute label.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Field
constructor
A new instance of Field.
Constructor Details
#initialize(params = {}) ⇒ Field
Returns a new instance of Field.
83 84 85 |
# File 'lib/simple-printer.rb', line 83 def initialize(params={}) params.each { |k, v| send("#{k}=", v) } end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
45 46 47 |
# File 'lib/simple-printer.rb', line 45 def children @children end |
#key ⇒ Object
Returns the value of attribute key.
42 43 44 |
# File 'lib/simple-printer.rb', line 42 def key @key end |
#label ⇒ Object
Returns the value of attribute label.
43 44 45 |
# File 'lib/simple-printer.rb', line 43 def label @label end |
#value ⇒ Object
Returns the value of attribute value.
44 45 46 |
# File 'lib/simple-printer.rb', line 44 def value @value end |
Class Method Details
.make(spec:, object: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/simple-printer.rb', line 47 def self.make(spec:, object: nil) return spec if spec.kind_of?(Field) Field.new.tap do |field| case spec when Symbol field.key = spec when Array spec = spec.dup field.key = spec.shift if spec.first.kind_of?(Symbol) field.label = spec.shift if spec.first.kind_of?(String) case (v = spec.shift) when Array field.children = v else field.value = v end when Hash field.key, field.label, field.value, field.children = spec[:key], spec[:label], spec[:value], spec[:children] else raise "Unknown field specification: #{spec.inspect}" end if field.key field.label ||= field.key.to_s.tr('_', ' ').capitalize if field.value.nil? raise "No object specified" unless object v = object.send(field.key) if v.kind_of?(Array) field.children = v else field.value = v end end end end end |