Class: ArrayFields::FieldSet
- Inherits:
-
Object
- Object
- ArrayFields::FieldSet
- Defined in:
- lib/arrayfields.rb
Overview
multiton cache of fields - wraps fields and fieldpos map to save memory
Instance Attribute Summary collapse
-
#fieldpos ⇒ Object
readonly
Returns the value of attribute fieldpos.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(fields) ⇒ FieldSet
constructor
A new instance of FieldSet.
- #pos(f) ⇒ Object
Constructor Details
#initialize(fields) ⇒ FieldSet
Returns a new instance of FieldSet.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/arrayfields.rb', line 40 def initialize fields raise ArgumentError, "<#{ fields.inspect }> not inject-able" unless fields.respond_to? :inject @fieldpos = fields.inject({}) do |h, f| unless String === f or Symbol === f raise ArgumentError, "<#{ f.inspect }> neither String nor Symbol" end h[f] = h.size h end @fields = fields end |
Instance Attribute Details
#fieldpos ⇒ Object (readonly)
Returns the value of attribute fieldpos.
39 40 41 |
# File 'lib/arrayfields.rb', line 39 def fieldpos @fieldpos end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
38 39 40 |
# File 'lib/arrayfields.rb', line 38 def fields @fields end |
Class Method Details
.init_sets ⇒ Object
31 32 33 |
# File 'lib/arrayfields.rb', line 31 def init_sets @sets = {} end |
.new(fields) ⇒ Object
28 29 30 |
# File 'lib/arrayfields.rb', line 28 def new fields @sets[fields] ||= super end |
Instance Method Details
#pos(f) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/arrayfields.rb', line 55 def pos f return @fieldpos[f] if @fieldpos.has_key? f f = f.to_s return @fieldpos[f] if @fieldpos.has_key? f f = f.intern return @fieldpos[f] if @fieldpos.has_key? f nil end |