Class: Sycl::Array
- Inherits:
-
Array
- Object
- Array
- Sycl::Array
- Defined in:
- lib/sycl.rb
Overview
A Sycl::Array is like an Array, but creating one from an array blesses any child Array or Hash objects into Sycl::Array or Sycl::Hash objects.
Sycl::Arrays support YAML preprocessing and postprocessing, and having individual nodes marked as being rendered in inline style. YAML output is also always sorted.
Defined Under Namespace
Classes: MockNativeType
Class Method Summary collapse
Instance Method Summary collapse
- #<<(e) ⇒ Object
-
#[]=(*args) ⇒ Object
Make sure that if we write to this array, we promote any inputs to their Sycl equivalents.
- #collect!(&block) ⇒ Object (also: #map!)
- #concat(a) ⇒ Object
- #encode_with(coder) ⇒ Object
- #fill(*args, &block) ⇒ Object
-
#initialize(*args) ⇒ Array
constructor
A new instance of Array.
- #insert(i, *args) ⇒ Object
- #method(sym) ⇒ Object
- #push(*args) ⇒ Object
-
#render_inline! ⇒ Object
Make this array, or its children, rendered in inline/flow style YAML.
- #render_values_inline! ⇒ Object
- #replace(a) ⇒ Object
- #to_yaml(opts = {}) ⇒ Object
- #unshift(*args) ⇒ Object
- #yaml_postprocess(yaml) ⇒ Object
- #yaml_postprocessor(&block) ⇒ Object
- #yaml_preprocess! ⇒ Object
-
#yaml_preprocessor(&block) ⇒ Object
Hooks to run before and after YAML dumping.
Constructor Details
#initialize(*args) ⇒ Array
Returns a new instance of Array.
81 82 83 84 85 86 |
# File 'lib/sycl.rb', line 81 def initialize(*args) @yaml_preprocessor = nil @yaml_postprocessor = nil @yaml_style = nil super end |
Class Method Details
.[](*args) ⇒ Object
88 89 90 |
# File 'lib/sycl.rb', line 88 def self.[](*args) Sycl::Array.from_array super end |
.from_array(a) ⇒ Object
96 97 98 99 100 |
# File 'lib/sycl.rb', line 96 def self.from_array(a) retval = Sycl::Array.new a.each { |e| retval << Sycl::from_object(e) } retval end |
.load_file(f) ⇒ Object
92 93 94 |
# File 'lib/sycl.rb', line 92 def self.load_file(f) Sycl::Array.from_array YAML::load_file f end |
Instance Method Details
#<<(e) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/sycl.rb', line 115 def <<(e) unless e.is_a?(Sycl::Hash) || e.is_a?(Sycl::Array) e = Sycl::from_object(e) end super end |
#[]=(*args) ⇒ Object
Make sure that if we write to this array, we promote any inputs to their Sycl equivalents. This lets dot notation, styled YAML, and other Sycl goodies continue.
107 108 109 110 111 112 113 |
# File 'lib/sycl.rb', line 107 def []=(*args) raise ArgumentError => 'wrong number of arguments' unless args.size > 1 unless args[-1].is_a?(Sycl::Hash) || args[-1].is_a?(Sycl::Array) args[-1] = Sycl::from_object(args[-1]) end super end |
#collect!(&block) ⇒ Object Also known as: map!
122 123 124 |
# File 'lib/sycl.rb', line 122 def collect!(&block) super { |o| Sycl::from_object(block.call o) } end |
#concat(a) ⇒ Object
127 128 129 130 |
# File 'lib/sycl.rb', line 127 def concat(a) a = Sycl::Array.from_array(a) unless a.is_a?(Sycl::Array) super end |
#encode_with(coder) ⇒ Object
234 235 236 237 |
# File 'lib/sycl.rb', line 234 def encode_with(coder) coder.style = Psych::Nodes::Sequence::FLOW if @yaml_style == :inline coder.represent_seq nil, sort end |
#fill(*args, &block) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sycl.rb', line 132 def fill(*args, &block) raise ArgumentError => 'wrong number of arguments' if args.empty? if block_given? super { |idx| Sycl::from_object(block.call idx) } else unless args[0].is_a?(Sycl::Hash) || args[0].is_a?(Sycl::Array) args[0] = Sycl::from_object(args[0]) end super end end |
#insert(i, *args) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/sycl.rb', line 144 def insert(i, *args) raise ArgumentError => 'wrong number of arguments' if args.empty? args.collect! do |o| unless o.is_a?(Sycl::Hash) || o.is_a?(Sycl::Array) o = Sycl::from_object(o) end end super end |
#method(sym) ⇒ Object
223 224 225 |
# File 'lib/sycl.rb', line 223 def method(sym) sym == :to_yaml ? MockNativeType.new : super end |
#push(*args) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/sycl.rb', line 154 def push(*args) raise ArgumentError => 'wrong number of arguments' if args.empty? args.collect! do |o| unless o.is_a?(Sycl::Hash) || o.is_a?(Sycl::Array) o = Sycl::from_object(o) end end super end |
#render_inline! ⇒ Object
Make this array, or its children, rendered in inline/flow style YAML. The default is to render arrays in block (multi-line) style.
183 184 185 |
# File 'lib/sycl.rb', line 183 def render_inline! @yaml_style = :inline end |
#render_values_inline! ⇒ Object
187 188 189 190 191 |
# File 'lib/sycl.rb', line 187 def render_values_inline! self.each do |e| e.render_inline! if e.respond_to?(:render_inline!) end end |
#replace(a) ⇒ Object
164 165 166 167 |
# File 'lib/sycl.rb', line 164 def replace(a) a = Sycl::Array.from_array(a) unless a.is_a?(Sycl::Array) super end |
#to_yaml(opts = {}) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/sycl.rb', line 240 def to_yaml(opts = {}) yaml_preprocess! if defined?(YAML::ENGINE) && YAML::ENGINE.yamler == 'psych' opts ||= {} opts[:line_width] ||= 999999 yaml = super else yaml = YAML::quick_emit(self, opts) do |out| out.seq(nil, @yaml_style || to_yaml_style) do |seq| sort.each { |e| seq.add(e) } end end end yaml_postprocess yaml end |
#unshift(*args) ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/sycl.rb', line 169 def unshift(*args) raise ArgumentError => 'wrong number of arguments' if args.empty? args.collect! do |o| unless o.is_a?(Sycl::Hash) || o.is_a?(Sycl::Array) o = Sycl::from_object(o) end end super end |
#yaml_postprocess(yaml) ⇒ Object
208 209 210 |
# File 'lib/sycl.rb', line 208 def yaml_postprocess(yaml) @yaml_postprocessor ? @yaml_postprocessor.call(yaml) : yaml end |
#yaml_postprocessor(&block) ⇒ Object
200 201 202 |
# File 'lib/sycl.rb', line 200 def yaml_postprocessor(&block) @yaml_postprocessor = block if block_given? end |
#yaml_preprocess! ⇒ Object
204 205 206 |
# File 'lib/sycl.rb', line 204 def yaml_preprocess! @yaml_preprocessor.call(self) if @yaml_preprocessor end |
#yaml_preprocessor(&block) ⇒ Object
Hooks to run before and after YAML dumping
196 197 198 |
# File 'lib/sycl.rb', line 196 def yaml_preprocessor(&block) @yaml_preprocessor = block if block_given? end |