Sha256: e798fa697d2d6cc561728846b45f1e787b5fefa0427e7a2e2af83237e315ed9c

Contents?: true

Size: 1.44 KB

Versions: 49

Compression:

Stored size: 1.44 KB

Contents

require 'albacore/paths'

module Albacore
  # a small DSL to mix into your configuration classes
  module ConfigDSL
    # creates a new attr_writer for the symbols passed,
    # such that a write to that method will normalise the paths
    # of the written value: you can pass an optional callback
    def attr_path *syms, &block
      given = block_given?

      syms.each do |sym|

        # this is the callback method when the value is set
        self.send(:define_method, :"__on_#{sym}") do |val|
          instance_exec(val, &block) if given
        end

        # this is the setter, it also calls the callback method
        # defined above.
        self.class_eval(
%{def #{sym}= val
  @#{sym} = ::Albacore::Paths.normalise_slashes val
  __on_#{sym} @#{sym}
end})
      end 
    end

    # read/write attribute with rewriting of set values to
    # match the system's paths
    def attr_path_accessor *syms, &block
      given = block_given?

      syms.each do |sym|

        # this is the callback method when the value is set
        self.send(:define_method, :"__on_#{sym}") do |val|
          instance_exec(val, &block) if given
        end

        # this is the setter and getter. The setter also calls
        # the callback method defined above.
        self.class_eval(
%{def #{sym}= val
  @#{sym} = ::Albacore::Paths.normalise_slashes val
  __on_#{sym} @#{sym}
end})
        self.class_eval(
%{def #{sym}
  @#{sym}
end})
      end 
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
albacore-3.0.1 ./lib/albacore/config_dsl.rb
albacore-3.0.0.pre.alpha.6 ./lib/albacore/config_dsl.rb
albacore-3.0.0.pre.alpha.5 ./lib/albacore/config_dsl.rb
albacore-3.0.0.pre.alpha.4 ./lib/albacore/config_dsl.rb
albacore-3.0.0.pre.alpha.3 ./lib/albacore/config_dsl.rb
albacore-3.0.0.pre.alpha.2 ./lib/albacore/config_dsl.rb
albacore-3.0.0.pre.alpha ./lib/albacore/config_dsl.rb
albacore-2.8.0 ./lib/albacore/config_dsl.rb
albacore-2.7.0 ./lib/albacore/config_dsl.rb
albacore-2.6.8 ./lib/albacore/config_dsl.rb
albacore-2.6.7 ./lib/albacore/config_dsl.rb
albacore-2.6.6 ./lib/albacore/config_dsl.rb
albacore-2.6.4 ./lib/albacore/config_dsl.rb
albacore-2.6.3 ./lib/albacore/config_dsl.rb
albacore-2.6.2 ./lib/albacore/config_dsl.rb
albacore-2.6.1 ./lib/albacore/config_dsl.rb
albacore-2.6.0 ./lib/albacore/config_dsl.rb
albacore-2.5.14 ./lib/albacore/config_dsl.rb
albacore-2.5.13 ./lib/albacore/config_dsl.rb
albacore-2.5.11 ./lib/albacore/config_dsl.rb