Class: Norikra::FieldSet
- Inherits:
-
Object
- Object
- Norikra::FieldSet
- Defined in:
- lib/norikra/fieldset.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#level ⇒ Object
Returns the value of attribute level.
-
#query_unique_keys ⇒ Object
Returns the value of attribute query_unique_keys.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
-
.field_names_key(data, fieldset = nil, strict = false, additional_fields = []) ⇒ Object
field_names_key is for lookup FieldSet from event data field_names_key: a,b,c,d comma separated field names, which contains valid values (null fields are not included).
- .leaves(container) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #bind(target, level, type_name_update = false) ⇒ Object
- #definition ⇒ Object
- #dup ⇒ Object
- #event_type_name ⇒ Object
- #field_names_key ⇒ Object
- #format(data) ⇒ Object
-
#initialize(fields, default_optional = nil, rebounds = 0, query_unique_keys = []) ⇒ FieldSet
constructor
fieldset doesn’t have container fields.
-
#nullable_diff(fieldset) ⇒ Object
data_fieldset.nullable_diff(query_fieldset).
- #rebind(type_name_update, query_fieldset = nil) ⇒ Object
-
#subset?(other) ⇒ Boolean
self is subset of other (or not) ### query_fieldset.subset?(fieldset_referred_from_event_data).
- #update(fields, optional_flag) ⇒ Object
-
#update_summary ⇒ Object
summary is for checks whether FieldSet is already registered or not.
Constructor Details
#initialize(fields, default_optional = nil, rebounds = 0, query_unique_keys = []) ⇒ FieldSet
fieldset doesn’t have container fields
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 |
# File 'lib/norikra/fieldset.rb', line 10 def initialize(fields, default_optional=nil, rebounds=0, query_unique_keys=[]) @fields = {} # fields.keys are raw key for container access chains fields.keys.each do |key| data = fields[key] if data.is_a?(Norikra::Field) @fields[data.name] = data elsif data.is_a?(Hash) type = data[:type].to_s optional = data.has_key?(:optional) ? data[:optional] : default_optional @fields[key.to_s] = Field.new(key.to_s, type, optional, !!data[:nullable]) elsif data.is_a?(String) || data.is_a?(Symbol) @fields[key.to_s] = Field.new(key.to_s, data.to_s, default_optional) else raise ::ArgumentError, "FieldSet.new argument class unknown: #{fields.class}" end end self.update_summary @target = nil @level = nil @rebounds = rebounds @query_unique_keys = query_unique_keys @event_type_name = nil end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
6 7 8 |
# File 'lib/norikra/fieldset.rb', line 6 def fields @fields end |
#level ⇒ Object
Returns the value of attribute level.
7 8 9 |
# File 'lib/norikra/fieldset.rb', line 7 def level @level end |
#query_unique_keys ⇒ Object
Returns the value of attribute query_unique_keys.
7 8 9 |
# File 'lib/norikra/fieldset.rb', line 7 def query_unique_keys @query_unique_keys end |
#summary ⇒ Object
Returns the value of attribute summary.
6 7 8 |
# File 'lib/norikra/fieldset.rb', line 6 def summary @summary end |
#target ⇒ Object
Returns the value of attribute target.
7 8 9 |
# File 'lib/norikra/fieldset.rb', line 7 def target @target end |
Class Method Details
.field_names_key(data, fieldset = nil, strict = false, additional_fields = []) ⇒ Object
field_names_key is for lookup FieldSet from event data field_names_key: a,b,c,d comma separated field names, which contains valid values (null fields are not included)
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 |
# File 'lib/norikra/fieldset.rb', line 81 def self.field_names_key(data, fieldset=nil, strict=false, additional_fields=[]) if !fieldset && strict raise RuntimeError, "strict(true) cannot be specified with fieldset=nil" end unless fieldset return data.reject{|k,v| v.respond_to?(:nullable?) && v.nullable? }.keys.sort.join(',') end keys = [] optionals = [] fieldset.fields.each do |key,field| if field.optional? optionals.push(field.name) else keys.push(field.name) end end optionals += additional_fields Norikra::FieldSet.leaves(data).each do |chain| value = chain.pop key = Norikra::Field.regulate_key_chain(chain).join('.') unless keys.include?(key) if optionals.include?(key) || (!strict && chain.size == 1) keys.push(key) end end end keys.sort.join(',') end |
.leaves(container) ⇒ Object
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 |
# File 'lib/norikra/fieldset.rb', line 41 def self.leaves(container) unless container.is_a?(Array) || container.is_a?(Hash) raise ::ArgumentError, "FieldSet#leaves accepts Array or Hash only" end return [] if container.empty? # returns list of [ [key-chain-items-flatten-list, value] ] dig = Proc.new do |obj| if obj.is_a?(Array) ary = [] obj.each_with_index do |v,i| if v.is_a?(Hash) || v.is_a?(Array) ary += dig.call(v).map{|chain| [i] + chain} else ary.push([i, v]) end end ary else # Hash obj.map {|k,v| if k.nil? [] elsif v.is_a?(Hash) || v.is_a?(Array) if v.empty? [] else dig.call(v).map{|chain| [k] + chain} end else [[k, v]] end }.reduce(:+) end end dig.call(container) end |
Instance Method Details
#==(other) ⇒ Object
136 137 138 139 |
# File 'lib/norikra/fieldset.rb', line 136 def ==(other) return false if self.class != other.class self.summary == other.summary && self.query_unique_keys == other.query_unique_keys end |
#bind(target, level, type_name_update = false) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/norikra/fieldset.rb', line 158 def bind(target, level, type_name_update=false) @target = target @level = level prefix = case level when :base then 'b_' when :query then 'q_' when :data then 'e_' # event else raise ::ArgumentError, "unknown fieldset bind level: #{level}, for target #{target}" end @rebounds += 1 if type_name_update query_unique_key = @query_unique_keys ? @query_unique_keys.join("\t") : '' @event_type_name = prefix + Digest::MD5.hexdigest([target, level.to_s, @rebounds.to_s, query_unique_key, @summary].join("\t")) self end |
#definition ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/norikra/fieldset.rb', line 141 def definition d = {} @fields.each do |key, field| d[field.escaped_name] = field.esper_type end d end |
#dup ⇒ Object
36 37 38 39 |
# File 'lib/norikra/fieldset.rb', line 36 def dup fields = Hash[@fields.map{|key,field| [key, {type: field.type, optional: field.optional, nullable: field.nullable?}]}] self.class.new(fields, nil, @rebounds, @query_unique_keys) end |
#event_type_name ⇒ Object
154 155 156 |
# File 'lib/norikra/fieldset.rb', line 154 def event_type_name @event_type_name.dup end |
#field_names_key ⇒ Object
115 116 117 |
# File 'lib/norikra/fieldset.rb', line 115 def field_names_key self.class.field_names_key(@fields) end |
#format(data) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/norikra/fieldset.rb', line 184 def format(data) # all keys of data should be already known at #format (before #format, do #refer) ret = {} @fields.each do |key,field| ret[field.escaped_name] = field.format(field.value(data)) end ret end |
#nullable_diff(fieldset) ⇒ Object
data_fieldset.nullable_diff(query_fieldset)
132 133 134 |
# File 'lib/norikra/fieldset.rb', line 132 def nullable_diff(fieldset) # data_fieldset.nullable_diff(query_fieldset) fieldset.fields.select{|fname, f| !self.fields[fname] && f.nullable? }.values end |
#rebind(type_name_update, query_fieldset = nil) ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/norikra/fieldset.rb', line 175 def rebind(type_name_update, query_fieldset=nil) renew = self.dup if query_fieldset diff = self.nullable_diff(query_fieldset) renew.update(diff, true) unless diff.empty? # all nullable fields are optional end renew.bind(@target, @level, type_name_update) end |
#subset?(other) ⇒ Boolean
self is subset of other (or not) ### query_fieldset.subset?(fieldset_referred_from_event_data)
149 150 151 152 |
# File 'lib/norikra/fieldset.rb', line 149 def subset?(other) # self is subset of other (or not) ### query_fieldset.subset?(fieldset_referred_from_event_data) ### nullable fields can be ignored (to be updated later, with nullable fields ignored here) (self.fields.keys - other.fields.keys).reject{|fname| @fields[fname].nullable? }.size < 1 end |
#update(fields, optional_flag) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/norikra/fieldset.rb', line 125 def update(fields, optional_flag) fields.each do |field| @fields[field.name] = field.dup(optional_flag) end self.update_summary end |
#update_summary ⇒ Object
summary is for checks whether FieldSet is already registered or not
120 121 122 123 |
# File 'lib/norikra/fieldset.rb', line 120 def update_summary @summary = @fields.keys.sort.map{|k| f = @fields[k]; "#{f.escaped_name}:#{f.type}" + (f.nullable? ? ':nullable' : '')}.join(',') self end |