lib/hari/node/queries.rb in hari-0.0.4 vs lib/hari/node/queries.rb in hari-0.0.5
- old
+ new
@@ -1,34 +1,49 @@
+require 'hari/node/queries/relation'
+require 'hari/node/queries/type'
+
module Hari
class Node < Entity
module Queries
- autoload :Relation, 'hari/node/queries/relation'
- autoload :Type, 'hari/node/queries/type'
+ extend ActiveSupport::Concern
delegate :in, :out, to: :relation_query
- delegate :set, :set!, to: :set_query
- delegate :sorted_set, :sorted_set!, to: :sorted_set_query
- delegate :list, :list!, to: :list_query
- delegate :string, :string!, to: :string_query
+ Keys::TYPES.each do |key|
+ query_builder = Keys.const_get(key.camelize)
- private
+ define_method key do |name = nil, options = {}|
+ return super() unless name
- def set_query
- Keys::Set.new query_node
- end
+ query = query_builder.new(query_node, options)
+ query.send key, name
+ end
- def sorted_set_query
- Keys::SortedSet.new query_node
+ define_method "#{key}!" do |name, options = {}|
+ query = query_builder.new(query_node, options)
+ query.send "#{key}!", name
+ end
end
- def list_query
- Keys::List.new query_node
- end
+ included do
+ Keys::TYPES.each do |key|
+ define_singleton_method key do |name = nil, options = {}|
+ return super() unless name
- def string_query
- Keys::String.new query_node
+ define_method(name) { send key, name, options }
+ define_method("#{name}!") { send "#{key}!", name, options }
+
+ define_method "#{name}=" do |value|
+ data = send(name)
+ data.delete!
+
+ data.add *value
+ end
+ end
+ end
end
+
+ private
def relation_query
Queries::Relation::Start.new query_node
end