Sha256: 4e752992f75b9ca35190b503e7d3d4fab7624a1237d6eb3a3d2ebd4511d7c904

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
whipped-cream-0.0.1pre3 lib/whipped-cream/builder.rb
whipped-cream-0.0.1pre2 lib/whipped-cream/builder.rb
whipped-cream-0.0.1pre1 lib/whipped-cream/builder.rb