Module: YamlStore::BlockBased

Defined in:
lib/yaml_store/block_based.rb

Defined Under Namespace

Classes: Condition

Instance Method Summary collapse

Instance Method Details

#fetchObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yaml_store/block_based.rb', line 48

def fetch
  result = []
  records.each do |record|
    passes = conditions.inject(true) do |boolean, condition|
      boolean & condition.run(record)
    end
    
    result << record if passes
  end
  
  reset_conditions!
  result
end

#having(property, value = :any) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yaml_store/block_based.rb', line 22

def having(property, value=:any)
  case value
  when :any
    conditions << Condition.new { |record| record.has_property?(property) }
  when /^(<|<=|>|>=|!=|=)\s*(\d+\.?\d*)$/ # equality and comparison statements
    comparator = $1
    comparator *= 2 if comparator == '='
    operand    = $2.to_f
    conditions << Condition.new { |record| Numeric === record.send(property) && record.send(property).to_f.send(comparator, operand) }
  when Regexp
    conditions << Condition.new { |record| (record.send(property) || '') =~ value }
  when Array
    conditions << Condition.new { |record| value.include?(record.send(property)) }
  else
    conditions << Condition.new { |record| record.send(property) == value }
  end

  self  
end

#not_having(property, value = :any) ⇒ Object



42
43
44
45
46
# File 'lib/yaml_store/block_based.rb', line 42

def not_having(property, value=:any)
  having(property, value)
  conditions.last.negated = true
  self
end