Module: Flex::UtilityMethods

Included in:
Flex
Defined in:
lib/flex/utility_methods.rb

Instance Method Summary collapse

Instance Method Details

#build_bulk_string(document, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/flex/utility_methods.rb', line 77

def build_bulk_string(document, options={})
  case document
  when Hash
    bulk_string_from_hash(document, options)
  when Flex::ModelIndexer, Flex::ActiveModel
    bulk_string_from_flex(document, options)
  else
    raise NotImplementedError, "Unable to convert the document #{document.inspect} to a bulk string."
  end
end

#doc(*args) ⇒ Object



35
36
37
# File 'lib/flex/utility_methods.rb', line 35

def doc(*args)
  flex.doc(*args)
end

#dump_all(*vars, &block) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/flex/utility_methods.rb', line 50

def dump_all(*vars, &block)
  refresh_index(*vars)
  scan_all({:params => {:fields => '*,_source'}}, *vars) do |batch|
    batch.map!{|document| document.delete('_score'); document}
    block.call(batch)
  end
end

#dump_one(*vars) ⇒ Object

refresh and pull the full document from the index



59
60
61
62
63
64
# File 'lib/flex/utility_methods.rb', line 59

def dump_one(*vars)
  refresh_index(*vars)
  document = search_by_id({:params => {:fields => '*,_source'}}, *vars)
  document.delete('_score')
  document
end

#json2yaml(json) ⇒ Object



21
22
23
# File 'lib/flex/utility_methods.rb', line 21

def json2yaml(json)
  YAML.dump(MultiJson.decode(json))
end

#post_bulk_collection(collection, options = {}) ⇒ Object

You should use Flex.post_bulk_string if you have an already formatted bulk data-string

Raises:



67
68
69
70
71
72
73
74
75
# File 'lib/flex/utility_methods.rb', line 67

def post_bulk_collection(collection, options={})
  raise ArgumentError, "Array expected as :collection, got #{collection.inspect}" \
        unless collection.is_a?(Array)
  bulk_string = ''
  collection.each do |d|
    bulk_string << build_bulk_string(d, options)
  end
  post_bulk_string(:bulk_string => bulk_string) unless bulk_string.empty?
end

#reload!Object



29
30
31
32
33
# File 'lib/flex/utility_methods.rb', line 29

def reload!
  flex.variables.deep_merge! Conf.variables
  Templates.contexts.each {|c| c.flex.reload!}
  true
end

#scan_all(*vars, &block) ⇒ Object



43
44
45
46
47
48
# File 'lib/flex/utility_methods.rb', line 43

def scan_all(*vars, &block)
  flex.scan_search(:match_all, *vars) do |raw_result|
    batch = raw_result['hits']['hits']
    block.call(batch)
  end
end

#scan_search(*args, &block) ⇒ Object



39
40
41
# File 'lib/flex/utility_methods.rb', line 39

def scan_search(*args, &block)
  flex.scan_search(*args, &block)
end

#search(data, vars = {}) ⇒ Object



4
5
6
# File 'lib/flex/utility_methods.rb', line 4

def search(data, vars={})
  Template::Search.new(data).setup(Flex.flex).render(vars)
end

#slim_search(data, vars = {}) ⇒ Object

like Flex.search, but it will use the Flex::Template::SlimSearch instead



9
10
11
# File 'lib/flex/utility_methods.rb', line 9

def slim_search(data, vars={})
  Template::SlimSearch.new(data).setup(Flex.flex).render(vars)
end

#yaml2json(yaml) ⇒ Object



25
26
27
# File 'lib/flex/utility_methods.rb', line 25

def yaml2json(yaml)
  MultiJson.encode(YAML.load(yaml))
end