Sha256: 14c2a815451d6a468f11b597eba56abeb84912765ebe95d47daf0cd40d7e1d6f

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# Converters converts data in the xib files property bag to the NodeInfos property bag.
# The output key it will have is stored here. The converter can be created with a conversion block
# Converter.new :output {|v| Convert value v here...}

class Converter
  def initialize(output, &conversion)
    @output = output
    @conversion = conversion || proc {|v| v}
  end
  
  def props_for(value)
    converted_value = @conversion.call(value)
    if converted_value
      {@output, converted_value}
    end
  end
end

# MultiConverter is used for cases when a property in the xhib file's property bag needs multiple
# properties in JS
# Example: {'frameOrigin' => "{123, 456}"} should give {:top => 123, :bottom => 456}
# Done by: MultiConverter.new([:top, :bottom], /\{(\d+), (\d+)\}/) {|v| v.to_i}

class MultiConverter < Converter
  def initialize(outputs, regex, &conversion)
    @outputs = outputs
    @regex = regex
    @conversion = conversion || proc {|v| v}
  end
  
  def props_for(value)
    if match = @regex.match(value)
      Hash[@outputs.zip(match.captures.map(&@conversion))]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xibtoti-0.0.2 lib/converters.rb
xibtoti-0.0.1 lib/converters.rb