Sha256: 7ad3858f0f21fa0206efb99e682e2c63b0c270b2347ad531653b66fb7f450223

Contents?: true

Size: 946 Bytes

Versions: 4

Compression:

Stored size: 946 Bytes

Contents

require 'action_view/helpers'

module AngularSprinkles
  module Helpers
    module DirectiveHelper
      include ::ActionView::Helpers

      def directive(name, input = {}, options = {}, &block)
        raise TypeError unless [String, Symbol, Array].include?(name.class)
        raise TypeError unless input.is_a?(Hash)
        raise TypeError unless options.is_a?(Hash)

        input.symbolize_keys!
        options.symbolize_keys!

        tag_contents = block_given? ? capture(&block) : ''
        tag_attributes = {}
        tag_name = options[:tag] || :div

        directive_attributes = Hash[input.map { |k, v| [k, v.to_json] }]
        name_attributes = Hash[[name].flatten.product([''])]

        tag_attributes[:data] = [directive_attributes, name_attributes].compact.inject(&:merge)
        tag_attributes.merge!(options.slice(:class, :style))

        content_tag(tag_name, tag_contents, tag_attributes)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
angular_sprinkles-0.0.4 lib/angular_sprinkles/helpers/directive_helper.rb
angular_sprinkles-0.0.3 lib/angular_sprinkles/helpers/directive_helper.rb
angular_sprinkles-0.0.2 lib/angular_sprinkles/helpers/directive_helper.rb
angular_sprinkles-0.0.1 lib/angular_sprinkles/helpers/directive_helper.rb