class CiteProc::Selector
Attributes
cp2rb[R]
matcher[R]
rb2cp[R]
types[R]
conditions[R]
custom_matcher[R]
skip_conditions[R]
type[R]
Public Class Methods
new(attributes = nil)
click to toggle source
# File lib/citeproc/selector.rb, line 22 def initialize(attributes = nil) @conditions, @skip_conditions = {}, {} if block_given? @type = :ruby else unless attributes.nil? || attributes.empty? attributes.symbolize_keys.each_pair do |key, conditions| conditions = convert_conditions(conditions) if conditions.is_a?(Array) case key when :all, :any, :none @type = key @conditions.merge!(conditions) when :select, :include, :exclude @type = Selector.cp2rb[key.to_s] @conditions.merge!(conditions) when :skip, :quash @skip_conditions.merge!(conditions) else raise TypeError, "failed to create selector from #{key.inspect}" end end end end end
Public Instance Methods
custom_matcher?()
click to toggle source
# File lib/citeproc/selector.rb, line 71 def custom_matcher? defined?(@custom_matcher) end
Also aliased as: ruby?
empty?()
click to toggle source
# File lib/citeproc/selector.rb, line 67 def empty? type.nil? && skip_conditions.empty? end
initialize_copy(other)
click to toggle source
# File lib/citeproc/selector.rb, line 53 def initialize_copy(other) @type = other.type @conditions = other.conditions.deep_copy @skip_conditions = other.skip_conditions.deep_copy @custom_matcher = other end
matches?(item)
click to toggle source
# File lib/citeproc/selector.rb, line 77 def matches?(item) if custom_matcher? custom_matcher.call(item) else conditions.each_pair.send(matcher) do |field, value| item[field].to_s == value.to_s end end end
skip?(item)
click to toggle source
# File lib/citeproc/selector.rb, line 87 def skip?(item) if custom_matcher? || skip_conditions.empty? false # skips are ignored for custom matchers else skip_conditions.each_pair.all? do |field, value| item[field].to_s == value.to_s end end end
to_citeproc()
click to toggle source
# File lib/citeproc/selector.rb, line 101 def to_citeproc return nil if empty? || custom_matcher? cp = {} cp[Selector.rb2cp[type]] = conditions.map do |field, value| { 'field' => field.to_s, 'value' => value.to_s } end unless conditions.empty? cp['quash'] = skip_conditions.map do |field, value| { 'field' => field.to_s, 'value' => value.to_s } end unless skip_conditions.empty? cp end
to_json()
click to toggle source
# File lib/citeproc/selector.rb, line 116 def to_json ::JSON.dump(to_citeproc) end
to_proc()
click to toggle source
# File lib/citeproc/selector.rb, line 97 def to_proc Proc.new { |item| matches?(item) && !skip?(item) } end
type=(type)
click to toggle source
# File lib/citeproc/selector.rb, line 60 def type=(type) raise TypeError, "failed to set selector type to #{type.inspect}" unless type.respond_to(:to_sym) && Selector.types.include?(type.to_sym) @type = type.to_sym end
Private Instance Methods
convert_conditions(conditions)
click to toggle source
Converts a CiteProc-JS style conditions list into a conditions Hash.
# File lib/citeproc/selector.rb, line 128 def convert_conditions(conditions) Hash[conditions.map { |c| [c['field'], c['value']] }] end
matcher()
click to toggle source
# File lib/citeproc/selector.rb, line 122 def matcher Selector.matcher[type] || :all? end