Sha256: 7bc8b6ececf0855058b0a4d01f59bb9209c58b22074c86c9e5e195438b6818ea

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

module WhippedCream
  # Creates Plugin instances from a DSL
  class Builder
    def self.build(&block)
      builder = new(&block)
      builder.build
      builder.plugin
    end

    def self.from_file(path)
      contents = File.read(path)

      from_string(contents)
    end

    def self.from_string(string)
      builder = new(string)
      builder.build
      builder.plugin
    end

    attr_reader :block, :string

    def initialize(string = nil, &block)
      @block = block
      @string = string
    end

    def build
      @build ||= string ?
        instance_eval(string) :
        instance_eval(&block)
    end

    def plugin
      @plugin ||= Plugin.new
    end

    def button(name, options = {}, &block)
      options = options.merge({ block: block })
      plugin.controls << Button.new(name, options)
    end

    def camera
      plugin.camera = true
    end

    def helpers(&block)
      plugin.instance_eval(&block)
    end

    def name(string)
      plugin.name = string
    end

    def sensor(name, options = {}, &block)
      plugin.controls << Sensor.new(name, options, &block)
    end

    def switch(name, options = {})
      plugin.controls << Switch.new(name, options)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
whipped-cream-0.2.0.beta1 lib/whipped-cream/builder.rb
whipped-cream-0.1.1 lib/whipped-cream/builder.rb
whipped-cream-0.1.0 lib/whipped-cream/builder.rb
whipped-cream-0.0.1 lib/whipped-cream/builder.rb
whipped-cream-0.0.1pre5 lib/whipped-cream/builder.rb
whipped-cream-0.0.1pre4 lib/whipped-cream/builder.rb