Class: Pione::Lang::KeyedSequence
- Defined in:
- lib/pione/lang/keyed-sequence.rb
Overview
KeyedSequence is a sequence that have key and value pairs.
Instance Method Summary collapse
-
#concat(other) ⇒ Sequence
(also: #+)
Concatenate the sequence and another one.
-
#each ⇒ Object
Iterate each elements.
- #element_type(env) ⇒ Object
- #eval(env) ⇒ Object
-
#get(key) ⇒ Object
Get the element by the key.
- #index_type(env) ⇒ Object
- #inspect ⇒ Object
-
#put(key, val) ⇒ Object
Put the element to the sequecence.
- #textize ⇒ Object
Methods inherited from Sequence
#assertive?, #attribute, index_type, inherited, piece_class, piece_classes, #push, #set_annotation_type, set_index_type, #update_pieces, void, #void?
Methods included from Callable
Methods included from Util::Positionable
#line_and_column, #pos, #set_source_position
Methods inherited from Expr
#eval!, inherited, pione_type, set_pione_type, #to_s
Instance Method Details
#concat(other) ⇒ Sequence Also known as: +
Concatenate the sequence and another one.
16 17 18 |
# File 'lib/pione/lang/keyed-sequence.rb', line 16 def concat(other) set(pieces: pieces.merge(other.pieces)) end |
#each ⇒ Object
Iterate each elements.
38 39 40 41 42 43 44 |
# File 'lib/pione/lang/keyed-sequence.rb', line 38 def each if block_given? pieces.each {|key, val| yield set(pieces: {key => val})} else Enumerator.new(self, :each) end end |
#element_type(env) ⇒ Object
50 51 52 |
# File 'lib/pione/lang/keyed-sequence.rb', line 50 def element_type(env) pieces.values.first.pione_type(env) end |
#eval(env) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/pione/lang/keyed-sequence.rb', line 54 def eval(env) _pieces = pieces.inject({}) do |_pieces, (key, val)| _pieces.update({key => val.map{|v| v.eval(env)}}) end set(pieces: _pieces) end |
#get(key) ⇒ Object
Get the element by the key.
22 23 24 |
# File 'lib/pione/lang/keyed-sequence.rb', line 22 def get(key) pieces[key] || (raise IndexError.new(key)) end |
#index_type(env) ⇒ Object
46 47 48 |
# File 'lib/pione/lang/keyed-sequence.rb', line 46 def index_type(env) pieces.keys.first.pione_type(env) end |
#inspect ⇒ Object
65 66 67 68 69 |
# File 'lib/pione/lang/keyed-sequence.rb', line 65 def inspect name = "KeyedSequence" content = pieces.map {|key, val| "%s:(%s)" % [key.pieces.first.value, val.textize]}.join(",") "#<%s [%s]>" % [name, content] end |
#put(key, val) ⇒ Object
Put the element to the sequecence.
27 28 29 30 31 32 33 34 35 |
# File 'lib/pione/lang/keyed-sequence.rb', line 27 def put(key, val) raise ArgumentError.new(key) unless key.kind_of?(Sequence) raise ArgumentError.new(val) unless val.kind_of?(Sequence) begin set(pieces: pieces.merge({key => get(key) + val})) rescue IndexError set(pieces: pieces.merge({key => val})) end end |
#textize ⇒ Object
61 62 63 |
# File 'lib/pione/lang/keyed-sequence.rb', line 61 def textize inspect end |