Class: CiteProc::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/citeproc/selector.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil, &block) ⇒ Selector

Returns a new instance of Selector.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/citeproc/selector.rb', line 22

def initialize(attributes = nil, &block)
  @conditions, @skip_conditions = {}, {}

  if block_given?
    @type = :ruby
    @custom_matcher = block

  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

Class Attribute Details

.cp2rb ⇒ Object (readonly)

Returns the value of attribute cp2rb.



17
18
19
# File 'lib/citeproc/selector.rb', line 17

def cp2rb
  @cp2rb
end

.matcher ⇒ Object (readonly)

Returns the value of attribute matcher.



17
18
19
# File 'lib/citeproc/selector.rb', line 17

def matcher
  @matcher
end

.rb2cp ⇒ Object (readonly)

Returns the value of attribute rb2cp.



17
18
19
# File 'lib/citeproc/selector.rb', line 17

def rb2cp
  @rb2cp
end

.types ⇒ Object (readonly)

Returns the value of attribute types.



17
18
19
# File 'lib/citeproc/selector.rb', line 17

def types
  @types
end

Instance Attribute Details

#conditions ⇒ Object (readonly)

Returns the value of attribute conditions.



20
21
22
# File 'lib/citeproc/selector.rb', line 20

def conditions
  @conditions
end

#custom_matcher ⇒ Object (readonly)

Returns the value of attribute custom_matcher.



20
21
22
# File 'lib/citeproc/selector.rb', line 20

def custom_matcher
  @custom_matcher
end

#skip_conditions ⇒ Object (readonly)

Returns the value of attribute skip_conditions.



20
21
22
# File 'lib/citeproc/selector.rb', line 20

def skip_conditions
  @skip_conditions
end

#type ⇒ Object

Returns the value of attribute type.



20
21
22
# File 'lib/citeproc/selector.rb', line 20

def type
  @type
end

Instance Method Details

#custom_matcher? ⇒ Boolean Also known as: ruby?

Returns:

  • (Boolean)


72
73
74
# File 'lib/citeproc/selector.rb', line 72

def custom_matcher?
  !custom_matcher.nil?
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/citeproc/selector.rb', line 68

def empty?
  type.nil? && skip_conditions.empty?
end

#initialize_copy(other) ⇒ Object



54
55
56
57
58
59
# File 'lib/citeproc/selector.rb', line 54

def initialize_copy(other)
  @type = other.type
  @conditions = other.conditions.deep_copy
  @skip_conditions = other.skip_conditions.deep_copy
  @custom_matcher = other.custom_matcher
end

#matches?(item) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
# File 'lib/citeproc/selector.rb', line 78

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) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/citeproc/selector.rb', line 88

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 ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/citeproc/selector.rb', line 102

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 ⇒ Object



117
118
119
# File 'lib/citeproc/selector.rb', line 117

def to_json
  ::JSON.dump(to_citeproc)
end

#to_proc ⇒ Object



98
99
100
# File 'lib/citeproc/selector.rb', line 98

def to_proc
  Proc.new { |item| matches?(item) && !skip?(item) }
end