Sha256: d8d99cd6ce2e36fae82be64eccaf3d5eaea9595a5879dd8e424c4a9bf230edfd

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module Verbalize
  class Build
    def self.call(required_keywords = [], optional_keywords = [])
      new(required_keywords, optional_keywords).call
    end

    def initialize(required_keywords, optional_keywords)
      @required_keywords = required_keywords
      @optional_keywords = optional_keywords
    end

    def call
      # We have to re-alias `!` to `call!` here, otherwise it will be pointing
      # to the original `call!` method
      <<-CODE
class << self
  def call(#{declaration_arguments_string})
    __proxied_call(#{forwarding_arguments_string})
  end

  def call!(#{declaration_arguments_string})
    __proxied_call!(#{forwarding_arguments_string})
  end
  alias_method :!, :call!
end

def initialize(#{declaration_arguments_string})
  #{initialize_body}
end

private

attr_reader #{attribute_readers_string}
      CODE
    end

    attr_reader :required_keywords, :optional_keywords

    private

    def all_keywords
      required_keywords + optional_keywords
    end

    def declaration_arguments_string
      required_segments = required_keywords.map { |keyword| "#{keyword}:" }
      optional_segments = optional_keywords.map { |keyword| "#{keyword}: nil" }
      (required_segments + optional_segments).join(', ')
    end

    def forwarding_arguments_string
      all_keywords.map { |keyword| "#{keyword}: #{keyword}" }.join(', ')
    end

    def initialize_body
      all_keywords.map { |keyword| "@#{keyword} = #{keyword}" }.join("\n  ")
    end

    def attribute_readers_string
      all_keywords.map { |keyword| ":#{keyword}" }.join(', ')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
verbalize-2.1.1 lib/verbalize/build.rb
verbalize-2.1.0 lib/verbalize/build.rb