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

Class Attribute Details

.cp2rbObject (readonly)

Returns the value of attribute cp2rb.



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

def cp2rb
  @cp2rb
end

.matcherObject (readonly)

Returns the value of attribute matcher.



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

def matcher
  @matcher
end

.rb2cpObject (readonly)

Returns the value of attribute rb2cp.



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

def rb2cp
  @rb2cp
end

.typesObject (readonly)

Returns the value of attribute types.



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

def types
  @types
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



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

def conditions
  @conditions
end

#custom_matcherObject (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_conditionsObject (readonly)

Returns the value of attribute skip_conditions.



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

def skip_conditions
  @skip_conditions
end

#typeObject

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)


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

def custom_matcher?
  defined?(@custom_matcher)
end

#empty?Boolean

Returns:

  • (Boolean)


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

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

#initialize_copy(other) ⇒ Object



53
54
55
56
57
58
# 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) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
# 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) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
# 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_citeprocObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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_jsonObject



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

def to_json
  ::JSON.dump(to_citeproc)
end

#to_procObject



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

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