Class: Repository::Criterion

Inherits:
Object
  • Object
show all
Defined in:
lib/repository/criterion.rb

Direct Known Subclasses

Conjunction, Contains, Equals, Join

Defined Under Namespace

Classes: And, Conjunction, Contains, Equals, Factory, Join, Key, Or

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Criterion

Returns a new instance of Criterion.



6
7
8
9
10
11
12
# File 'lib/repository/criterion.rb', line 6

def initialize(options)
  @subject = (options[:subject] || "id").to_s
  @descriptor = options[:descriptor] || "#{@subject} #{default_descriptor}"
  @value = options[:value]
  @value = nil if @value.blank?
  @property_name = options[:property_name] || @subject
end

Instance Attribute Details

#descriptorObject (readonly)

Returns the value of attribute descriptor.



4
5
6
# File 'lib/repository/criterion.rb', line 4

def descriptor
  @descriptor
end

#property_nameObject (readonly)

Returns the value of attribute property_name.



4
5
6
# File 'lib/repository/criterion.rb', line 4

def property_name
  @property_name
end

#subjectObject (readonly)

Returns the value of attribute subject.



4
5
6
# File 'lib/repository/criterion.rb', line 4

def subject
  @subject
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/repository/criterion.rb', line 4

def value
  @value
end

Instance Method Details

#+(other) ⇒ Object Also known as: &



49
50
51
# File 'lib/repository/criterion.rb', line 49

def +(other)
  And.new(self, other)
end

#==(other) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/repository/criterion.rb', line 14

def ==(other)
  self.class == other.class &&
  self.subject == other.subject &&
  self.descriptor == other.descriptor &&
  self.value == other.value &&
  self.property_name == other.property_name
end

#default_descriptorObject



26
27
28
# File 'lib/repository/criterion.rb', line 26

def default_descriptor
  self.class.name.gsub(/[A-Z]/, " \\0").gsub(/.*::/, '').downcase.strip
end

#described_valueObject



30
31
32
33
34
35
36
37
38
# File 'lib/repository/criterion.rb', line 30

def described_value
  if value.blank?
    "any"
  elsif value == 0
    "none"
  else
    value
  end
end

#descriptionObject



22
23
24
# File 'lib/repository/criterion.rb', line 22

def description
  "#{descriptor} #{described_value}"
end

#find_in(storage) ⇒ Object



59
60
61
# File 'lib/repository/criterion.rb', line 59

def find_in(storage)
  storage.find(self)
end

#match?(object) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/repository/criterion.rb', line 40

def match?(object)
  object_value = object.send(property_name)
  if value.is_a? Array
    value.detect{|criterion_value| match_value?(criterion_value, object_value)}
  else
    match_value?(value, object_value)
  end
end

#|(other) ⇒ Object



55
56
57
# File 'lib/repository/criterion.rb', line 55

def |(other)
  Or.new(self, other)
end