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
- .leaves(container) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
TODO: have a bug?.
- #bind(target, level, update_type_name = 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.
- #rebind(update_type_name) ⇒ Object
-
#subset?(other) ⇒ Boolean
self is subset of other (or not).
- #update(fields, optional_flag) ⇒ Object
- #update_summary ⇒ Object
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) 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
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 |
# File 'lib/norikra/fieldset.rb', line 78 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.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
TODO: have a bug?
129 130 131 132 |
# File 'lib/norikra/fieldset.rb', line 129 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, update_type_name = false) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/norikra/fieldset.rb', line 150 def bind(target, level, update_type_name=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 update_type_name 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
134 135 136 137 138 139 140 |
# File 'lib/norikra/fieldset.rb', line 134 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}]}] self.class.new(fields, nil, @rebounds, @query_unique_keys) end |
#event_type_name ⇒ Object
146 147 148 |
# File 'lib/norikra/fieldset.rb', line 146 def event_type_name @event_type_name.dup end |
#field_names_key ⇒ Object
112 113 114 |
# File 'lib/norikra/fieldset.rb', line 112 def field_names_key self.class.field_names_key(@fields) end |
#format(data) ⇒ Object
171 172 173 174 175 176 177 178 |
# File 'lib/norikra/fieldset.rb', line 171 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 |
#rebind(update_type_name) ⇒ Object
167 168 169 |
# File 'lib/norikra/fieldset.rb', line 167 def rebind(update_type_name) self.dup.bind(@target, @level, update_type_name) end |
#subset?(other) ⇒ Boolean
self is subset of other (or not)
142 143 144 |
# File 'lib/norikra/fieldset.rb', line 142 def subset?(other) # self is subset of other (or not) (self.fields.keys - other.fields.keys).size == 0 end |
#update(fields, optional_flag) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/norikra/fieldset.rb', line 121 def update(fields, optional_flag) fields.each do |field| @fields[field.name] = field.dup(optional_flag) end self.update_summary end |
#update_summary ⇒ Object
116 117 118 119 |
# File 'lib/norikra/fieldset.rb', line 116 def update_summary @summary = @fields.keys.sort.map{|k| @fields[k].escaped_name + ':' + @fields[k].type}.join(',') self end |