Sha256: c009f184546fd734b505243e84b1699510c487ccb124f770859fe6f2c23f882f

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module VCardio
  class Builder
    def initialize
      @builders   = []
    end

    def method_missing(method_name, *args)
      return super if respond_to?(method_name)
      builder = PropertyBuilder.new(method_name, args)
      @builders << builder
      builder
    end

    def properties
      properties = []

      @builders.each do |builder|
        properties << builder.to_property
      end

      properties
    end

    def self.call(&block)
      instance = new
      instance.instance_eval(&block)
      instance
    end

    class PropertyBuilder
      def initialize(name, args)
        @args  = args
        @group = nil
        @name  = name
      end

      def build_parameters(hash)
        hash ||= {}
        parameters = []

        hash.each do |k, v|
          parameters << VCardio::Parameter.new(k, v)
        end

        parameters
      end

      def to_property
        args       = @args.clone
        value      = args.shift
        parameters = build_parameters(args[0])

        VCardio::Property.new(@group, @name, parameters, value)
      end

      def method_missing(method_name, *args)
        @group = @name
        @name  = method_name
        @args  = args
        self
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vcardio-0.1.1 lib/vcardio/builder.rb
vcardio-0.1.0 lib/vcardio/builder.rb