Skip to content

Commit

Permalink
Merge pull request #172 from rsolr/dependencies
Browse files Browse the repository at this point in the history
Explicitly require load-time dependencies
  • Loading branch information
jcoyne committed Feb 24, 2017
2 parents dd95bd0 + a8965cd commit ba5ae27
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 89 deletions.
87 changes: 0 additions & 87 deletions lib/rsolr/document.rb
Expand Up @@ -55,91 +55,4 @@ def as_json
end
end
end

class Field

def self.instance(attrs, value)
attrs = attrs.dup
field_type = attrs.delete(:type) { value.class.name }

klass = if field_type.is_a? String
class_for_field(field_type)
elsif field_type.is_a? Class
field_type
else
self
end

klass.new(attrs, value)
end

def self.class_for_field(field_type)
potential_class_name = field_type + 'Field'.freeze
search_scope = Module.nesting[1]
search_scope.const_defined?(potential_class_name, false) ? search_scope.const_get(potential_class_name) : self
end
private_class_method :class_for_field

# "attrs" is a hash for setting the "doc" xml attributes
# "value" is the text value for the node
attr_accessor :attrs, :source_value

# "attrs" must be a hash
# "value" should be something that responds to #_to_s
def initialize(attrs, source_value)
@attrs = attrs
@source_value = source_value
end

# the value of the "name" attribute
def name
attrs[:name]
end

def value
source_value
end

def as_json
if attrs[:update]
{ attrs[:update] => value }
elsif attrs.any? { |k, _| k != :name }
hash = attrs.dup
hash.delete(:name)
hash.merge(value: value)
else
value
end
end
end

class DateField < Field
def value
Time.utc(source_value.year, source_value.mon, source_value.mday).iso8601
end
end

class TimeField < Field
def value
source_value.getutc.strftime('%FT%TZ')
end
end

class DateTimeField < Field
def value
source_value.to_time.getutc.iso8601
end
end

class DocumentField < Field
def value
return RSolr::Document.new(source_value) if source_value.respond_to? :each_pair

super
end

def as_json
value.as_json
end
end
end
87 changes: 87 additions & 0 deletions lib/rsolr/field.rb
@@ -0,0 +1,87 @@
module RSolr
class Field
def self.instance(attrs, value)
attrs = attrs.dup
field_type = attrs.delete(:type) { value.class.name }

klass = if field_type.is_a? String
class_for_field(field_type)
elsif field_type.is_a? Class
field_type
else
self
end

klass.new(attrs, value)
end

def self.class_for_field(field_type)
potential_class_name = field_type + 'Field'.freeze
search_scope = Module.nesting[1]
search_scope.const_defined?(potential_class_name, false) ? search_scope.const_get(potential_class_name) : self
end
private_class_method :class_for_field

# "attrs" is a hash for setting the "doc" xml attributes
# "value" is the text value for the node
attr_accessor :attrs, :source_value

# "attrs" must be a hash
# "value" should be something that responds to #_to_s
def initialize(attrs, source_value)
@attrs = attrs
@source_value = source_value
end

# the value of the "name" attribute
def name
attrs[:name]
end

def value
source_value
end

def as_json
if attrs[:update]
{ attrs[:update] => value }
elsif attrs.any? { |k, _| k != :name }
hash = attrs.dup
hash.delete(:name)
hash.merge(value: value)
else
value
end
end
end

class DateField < Field
def value
Time.utc(source_value.year, source_value.mon, source_value.mday).iso8601
end
end

class TimeField < Field
def value
source_value.getutc.strftime('%FT%TZ')
end
end

class DateTimeField < Field
def value
source_value.to_time.getutc.iso8601
end
end

class DocumentField < Field
def value
return RSolr::Document.new(source_value) if source_value.respond_to? :each_pair

super
end

def as_json
value.as_json
end
end
end
1 change: 1 addition & 0 deletions lib/rsolr/json.rb
@@ -1,4 +1,5 @@
require 'json'
require 'rsolr/generator'

module RSolr::JSON
class Generator < RSolr::Generator
Expand Down
4 changes: 2 additions & 2 deletions lib/rsolr/xml.rb
@@ -1,6 +1,6 @@
require 'rsolr/generator'
require 'rsolr/document'
module RSolr::Xml
require 'rsolr/document'

Document = RSolr::Document
Field = RSolr::Field

Expand Down

0 comments on commit ba5ae27

Please sign in to comment.