Sha256: 2b3d5dfa6c18a33d43c20cd517455893325bfcda60c01f9accebde244c089960

Contents?: true

Size: 1.48 KB

Versions: 56

Compression:

Stored size: 1.48 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

56 entries across 56 versions & 1 rubygems

Version Path
albacore-2.3.7 lib/albacore/config_dsl.rb
albacore-2.3.6 lib/albacore/config_dsl.rb
albacore-2.3.5 lib/albacore/config_dsl.rb
albacore-2.3.4 lib/albacore/config_dsl.rb
albacore-2.3.3 lib/albacore/config_dsl.rb
albacore-2.3.2 lib/albacore/config_dsl.rb
albacore-2.3.1 lib/albacore/config_dsl.rb
albacore-2.3.0 lib/albacore/config_dsl.rb
albacore-2.2.1 lib/albacore/config_dsl.rb
albacore-2.2.0 lib/albacore/config_dsl.rb
albacore-2.2.0.pre.beta.7 lib/albacore/config_dsl.rb
albacore-2.2.0.pre.beta.6 lib/albacore/config_dsl.rb
albacore-2.2.0.pre.beta.5 lib/albacore/config_dsl.rb
albacore-2.2.0.pre.beta.4 lib/albacore/config_dsl.rb
albacore-2.2.0.pre.beta.3 lib/albacore/config_dsl.rb
albacore-2.2.0.pre.beta.2 lib/albacore/config_dsl.rb
albacore-2.2.0.pre.beta lib/albacore/config_dsl.rb
albacore-2.1.2 lib/albacore/config_dsl.rb
albacore-2.1.1 lib/albacore/config_dsl.rb
albacore-2.0.16 lib/albacore/config_dsl.rb