Sha256: 177ff9c42e89a8141c32cf3923a64734bbcdcd4a9393829f98ecfa239a5db0fc

Contents?: true

Size: 1.34 KB

Versions: 19

Compression:

Stored size: 1.34 KB

Contents

module CSL
  class Style

    class Choose < Node

      alias blocks children

      class Block < Node
        attr_struct :match, *Schema.attr(:conditionals)

        attr_defaults :match => 'all'

        class << self
          def matches?(nodename)
            nodename === ':if' || nodename === ':elseif' || nodename === ':else'
          end
        end

        def conditions
          attributes_for(*Schema.attr(:conditionals)).map do |name, values|
            extract_type_and_matcher_from(name) << values.to_s.split(/\s+/)
          end
        end

        def matcher(match = attributes[:match])
          case match.to_s
          when 'any'
            :any?
          when 'none'
            :none?
          else
            :all?
          end
        end

        private

        def extract_type_and_matcher_from(attribute)
          type, match = attribute.to_s.split(/-(any|all|none)$/, 2)

          # subtle: if the default matcher is :none? and there
          # is no override we want to use :any? inside the nested
          # lists to avoid double negation during evaluation of
          # the entire expression!

          if match.nil?
            match = matcher
            [type.to_sym, match == :none? ? :any? : matcher]
          else
            [type.to_sym, matcher(match)]
          end
        end
      end
    end

  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
csl-2.0.0 lib/csl/style/choose.rb
csl-1.6.0 lib/csl/style/choose.rb
csl-1.5.2 lib/csl/style/choose.rb
csl-1.5.1 lib/csl/style/choose.rb
csl-1.5.0 lib/csl/style/choose.rb
csl-1.4.5 lib/csl/style/choose.rb
csl-1.4.4 lib/csl/style/choose.rb
csl-1.4.3 lib/csl/style/choose.rb
csl-1.4.2 lib/csl/style/choose.rb
csl-1.4.1 lib/csl/style/choose.rb
csl-1.4.0 lib/csl/style/choose.rb
csl-1.3.2 lib/csl/style/choose.rb
csl-1.3.1 lib/csl/style/choose.rb
csl-1.3.0 lib/csl/style/choose.rb
csl-1.2.3 lib/csl/style/choose.rb
csl-1.2.2 lib/csl/style/choose.rb
csl-1.2.1 lib/csl/style/choose.rb
csl-1.2.0 lib/csl/style/choose.rb
csl-1.1.0 lib/csl/style/choose.rb