Class: MotionPrime::Styles

Inherits:
Object
  • Object
show all
Extended by:
HasNormalizer
Defined in:
motion-prime/views/styles.rb

Constant Summary collapse

@@repo =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasNormalizer

debug_info, element?, normalize_object, normalize_options, normalize_value

Constructor Details

#initialize(namespace = nil) ⇒ Styles

Returns a new instance of Styles.



6
7
8
# File 'motion-prime/views/styles.rb', line 6

def initialize(namespace = nil)
  @namespace = namespace
end

Class Method Details

.define(*namespaces, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'motion-prime/views/styles.rb', line 42

def define(*namespaces, &block)
  @definition_blocks ||= []
  namespaces = Array.wrap(namespaces)
  if namespaces.any?
    namespaces.each do |namespace|
      @definition_blocks << {namespace: namespace, block: block}
    end
  else
    @definition_blocks << {namespace: false, block: block}
  end
end

.define!Object



54
55
56
57
58
59
# File 'motion-prime/views/styles.rb', line 54

def define!
  @definition_blocks.each do |definition|
    block = definition[:block]
    self.new(definition[:namespace]).instance_eval(&block)
  end
end

.extend_and_normalize_options(options = {}) ⇒ Object



71
72
73
74
# File 'motion-prime/views/styles.rb', line 71

def extend_and_normalize_options(options = {})
  style_options = self.for(options.delete(:styles))
  normalize_options(style_options.deep_merge(options))
end

.for(style_names, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'motion-prime/views/styles.rb', line 61

def for(style_names, options = {})
  style_options = {}
  Array.wrap(style_names).each do |name|
    styles = @@repo[name]
    Prime.logger.debug "No styles found for `#{name}` (element: `#{options[:name]}`, type: #{options.fetch(:type, 'general')})" if options[:debug_missing] && styles.blank?
    style_options.deep_merge!(styles || {})
  end
  style_options
end

Instance Method Details

#style(*args, &block) ⇒ Object Also known as: _



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'motion-prime/views/styles.rb', line 10

def style(*args, &block)
  names = Array.wrap(args)
  options = names.pop if args.last.is_a?(Hash)

  if options.present?
    parent = options.delete(:parent)
    if parent && (parent_namespace = options.delete(:parent_namespace) || @namespace)
      parent ="#{parent_namespace}_#{parent}".to_sym
    end
    mixins = Array.wrap(options.delete(:mixins)).map { |mixin_name| :"_mixin_#{mixin_name}" }

    names.each do |name|
      name = "#{@namespace}_#{name}".to_sym if @namespace
      @@repo[name] ||= {}
      @@repo[name].deep_merge!(self.class.for(parent, debug_missing: true, type: :parent, name: name)) if parent
      @@repo[name].deep_merge!(self.class.for(mixins, debug_missing: true, type: :mixin, name: name)) if mixins.present?
      @@repo[name].deep_merge! options
    end
  elsif !block_given?
    raise "No style rules specified for `#{names.join(', ')}`. Namespace: `#{@namespace}`"
  end

  names.each do |name|
    namespace = [@namespace, name].compact.join('_')
    self.class.new(namespace).instance_eval(&block)
  end if block_given?
end