Module: YamlStore::EvalBased
- Included in:
- YamlStore
- Defined in:
- lib/yaml_store.rb
Instance Method Summary collapse
- #fetch(all_or_first = :all) ⇒ Object
- #having(property, value = :any) ⇒ Object
- #not_having(property, value = :any) ⇒ Object
Instance Method Details
#fetch(all_or_first = :all) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/yaml_store.rb', line 74 def fetch(all_or_first=:all) filter = conditions.empty?? 'true' : conditions.join(' && ') reset_conditions! method = all_or_first == :all ? :select : :detect records.send(method) do |record| eval(filter) end end |
#having(property, value = :any) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/yaml_store.rb', line 50 def having(property, value=:any) case value when :any conditions << "record.has_property?(#{property.inspect})" when /^(<|<=|>|>=|!=|=)\s*(\d+\.?\d*)$/ # equality and comparison statements value = "=#{value}" if $1 == '=' # ensure two equal signs (to allow for '= 7.1') conditions << "(Numeric === record.#{property} && record.#{property}.to_f #{value})" when Regexp conditions << "(record.#{property} || '') =~ #{value.inspect}" when Array conditions << "#{value.inspect}.include?(record.#{property})" else conditions << "record.#{property} == #{value.inspect}" end self end |
#not_having(property, value = :any) ⇒ Object
68 69 70 71 72 |
# File 'lib/yaml_store.rb', line 68 def not_having(property, value=:any) having(property, value) conditions[-1] = "!(#{conditions[-1]})" self end |