/
fields.rb
143 lines (109 loc) · 2.63 KB
/
fields.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'hammer_cli/output/dsl'
module Fields
class Field
attr_reader :path
attr_writer :sets
attr_accessor :label, :parent
def initialize(options={})
@hide_blank = options[:hide_blank].nil? ? false : options[:hide_blank]
@hide_missing = options[:hide_missing].nil? ? true : options[:hide_missing]
@path = options[:path] || []
@label = options[:label]
@sets = options[:sets]
@options = options
@parent = nil
end
def id
@options[:id] || @options[:key] || @label
end
def match_id?(field_id)
@options[:id] == field_id || @options[:key] == field_id || @label == _(field_id)
end
def hide_blank?
@hide_blank
end
def hide_missing?
@hide_missing
end
def full_label
return @label.to_s if @parent.nil?
"#{@parent.full_label}/#{@label}"
end
def sets
@sets || inherited_sets || default_sets
end
def display?(value)
if value.is_a?(HammerCLI::Output::DataMissing)
!hide_missing?
elsif value.nil?
!hide_blank?
else
true
end
end
def parameters
@options
end
protected
def inherited_sets
@parent&.sets
end
def default_sets
%w[DEFAULT ALL]
end
end
class ContainerField < Field
def initialize(options={}, &block)
super(options)
dsl = HammerCLI::Output::Dsl.new
dsl.build(&block) if block_given?
dsl.fields.each { |f| f.parent = self }
self.output_definition.append dsl.fields
end
def output_definition
@output_definition ||= HammerCLI::Output::Definition.new
@output_definition
end
def fields
@output_definition.fields
end
def display?(value)
if value.is_a?(HammerCLI::Output::DataMissing)
!hide_missing?
elsif value.nil? || value.empty?
!hide_blank?
else
true
end
end
end
class Date < Field
end
class Id < Field
end
class List < Field
end
class LongText < Field
end
class Text < Field
end
class KeyValue < Field
end
class Label < ContainerField
def display?(value)
return false if value.is_a?(HammerCLI::Output::DataMissing) && hide_missing?
return true if not hide_blank?
!(value.nil? || value.empty?) && fields.any? do |f|
f.display?(HammerCLI::Output::Adapter::Abstract.data_for_field(f, value))
end
end
end
class Collection < ContainerField
def initialize(options={}, &block)
options[:numbered] = true if options[:numbered].nil?
super(options, &block)
end
end
class Boolean < Field
end
end