flex 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/flex.rb +21 -8
- data/lib/flex/class_proxy/base.rb +15 -0
- data/lib/flex/class_proxy/loader.rb +102 -0
- data/lib/flex/{model/class_proxy.rb → class_proxy/model.rb} +5 -5
- data/lib/flex/{related_model/class_sync.rb → class_proxy/model_sync.rb} +3 -3
- data/lib/flex/{related_model/class_proxy.rb → class_proxy/related_model.rb} +3 -3
- data/lib/flex/{related_model/instance_proxy.rb → instance_proxy/base.rb} +3 -2
- data/lib/flex/{model/instance_proxy.rb → instance_proxy/model.rb} +11 -11
- data/lib/flex/instance_proxy/related_model.rb +7 -0
- data/lib/flex/loader.rb +2 -100
- data/lib/flex/model.rb +3 -3
- data/lib/flex/model_manager.rb +65 -0
- data/lib/flex/rails.rb +3 -3
- data/lib/flex/rails/helper.rb +1 -1
- data/lib/flex/related_model.rb +3 -3
- data/lib/flex/result/document.rb +1 -1
- data/lib/flex/tasks.rb +1 -1
- data/lib/flex/template.rb +1 -1
- metadata +11 -9
- data/lib/flex/class_proxy.rb +0 -12
- data/lib/flex/model/manager.rb +0 -67
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/flex.rb
CHANGED
@@ -5,8 +5,10 @@ require 'multi_json'
|
|
5
5
|
require 'flex/logger'
|
6
6
|
require 'flex/errors'
|
7
7
|
require 'flex/utils'
|
8
|
+
|
8
9
|
require 'flex/structure/indifferent_access'
|
9
10
|
require 'flex/structure/mergeable'
|
11
|
+
|
10
12
|
require 'flex/result'
|
11
13
|
require 'flex/result/collection'
|
12
14
|
require 'flex/result/document'
|
@@ -14,7 +16,9 @@ require 'flex/result/source_document'
|
|
14
16
|
require 'flex/result/search'
|
15
17
|
require 'flex/result/source_search'
|
16
18
|
require 'flex/result/bulk'
|
19
|
+
|
17
20
|
require 'flex/variables'
|
21
|
+
|
18
22
|
require 'flex/template/base'
|
19
23
|
require 'flex/template/partial'
|
20
24
|
require 'flex/template'
|
@@ -22,16 +26,23 @@ require 'flex/template/search'
|
|
22
26
|
require 'flex/template/slim_search'
|
23
27
|
require 'flex/template/tags'
|
24
28
|
require 'flex/template/info'
|
25
|
-
|
29
|
+
|
30
|
+
require 'flex/model_manager'
|
31
|
+
|
32
|
+
require 'flex/class_proxy/base'
|
33
|
+
require 'flex/class_proxy/loader'
|
34
|
+
require 'flex/class_proxy/model_sync'
|
35
|
+
require 'flex/class_proxy/model'
|
36
|
+
require 'flex/class_proxy/related_model'
|
37
|
+
|
38
|
+
require 'flex/instance_proxy/base'
|
39
|
+
require 'flex/instance_proxy/model'
|
40
|
+
require 'flex/instance_proxy/related_model'
|
41
|
+
|
26
42
|
require 'flex/loader'
|
27
|
-
require 'flex/model/manager'
|
28
43
|
require 'flex/related_model'
|
29
|
-
require 'flex/related_model/class_sync'
|
30
|
-
require 'flex/related_model/class_proxy'
|
31
|
-
require 'flex/related_model/instance_proxy'
|
32
44
|
require 'flex/model'
|
33
|
-
|
34
|
-
require 'flex/model/instance_proxy'
|
45
|
+
|
35
46
|
require 'flex/http_clients/patron'
|
36
47
|
require 'flex/http_clients/rest_client'
|
37
48
|
require 'flex/configuration'
|
@@ -39,7 +50,9 @@ require 'flex/utility_methods'
|
|
39
50
|
|
40
51
|
module Flex
|
41
52
|
|
42
|
-
VERSION
|
53
|
+
VERSION = File.read(File.expand_path('../../VERSION', __FILE__)).strip
|
54
|
+
LIB_PATH = __FILE__.sub(/flex.rb$/, '')
|
55
|
+
|
43
56
|
|
44
57
|
# The following lines are autogenerated by Flex.info
|
45
58
|
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Flex
|
2
|
+
module ClassProxy
|
3
|
+
class Loader < Base
|
4
|
+
attr_reader :templates, :partials
|
5
|
+
|
6
|
+
include Template::Info
|
7
|
+
|
8
|
+
def initialize(base)
|
9
|
+
super
|
10
|
+
@sources = []
|
11
|
+
@templates = {}
|
12
|
+
@partials = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
# accepts a path to a file or YAML string
|
16
|
+
def load_source_for(klass, source, source_vars)
|
17
|
+
if source.nil? || source != /\n/
|
18
|
+
paths = [ "#{Configuration.flex_dir}/#{source}.yml",
|
19
|
+
"#{Configuration.flex_dir}/#{ModelManager.class_name_to_type(host_class.name)}.yml",
|
20
|
+
source ]
|
21
|
+
source = paths.find {|p| File.exist?(p)}
|
22
|
+
end
|
23
|
+
raise ArgumentError, "expected a string (got #{source.inspect})." \
|
24
|
+
unless source.is_a?(String)
|
25
|
+
@sources << [klass, source, source_vars]
|
26
|
+
do_load_source(klass, source, source_vars)
|
27
|
+
end
|
28
|
+
|
29
|
+
# loads a Generic Template source
|
30
|
+
def load_source(source=nil, source_vars=nil)
|
31
|
+
load_source_for(Template, source, source_vars)
|
32
|
+
end
|
33
|
+
|
34
|
+
# loads a Search Template source
|
35
|
+
def load_search_source(source=nil, source_vars=nil)
|
36
|
+
load_source_for(Template::Search, source, source_vars)
|
37
|
+
end
|
38
|
+
|
39
|
+
# loads a SlimSearch Template source
|
40
|
+
def load_slim_search_source(source=nil, source_vars=nil)
|
41
|
+
load_source_for(Template::SlimSearch, source, source_vars)
|
42
|
+
end
|
43
|
+
|
44
|
+
# reloads the sources (useful in the console and used internally)
|
45
|
+
def reload!
|
46
|
+
@sources.each {|k,s,v| do_load_source(k,s,v)}
|
47
|
+
end
|
48
|
+
|
49
|
+
# adds a template instance and defines the template method in the host class
|
50
|
+
def add_template(name, template)
|
51
|
+
templates[name] = template
|
52
|
+
# no define_singleton_method in 1.8.7
|
53
|
+
host_class.instance_eval <<-ruby, __FILE__, __LINE__ + 1
|
54
|
+
def #{name}(vars={})
|
55
|
+
raise ArgumentError, "#{host_class}.#{name} expects a Hash (got \#{vars.inspect})" unless vars.is_a?(Hash)
|
56
|
+
#{host_class.respond_to?(:preprocess_variables) && 'preprocess_variables(vars)'}
|
57
|
+
flex.templates[:#{name}].render(vars)
|
58
|
+
end
|
59
|
+
ruby
|
60
|
+
end
|
61
|
+
|
62
|
+
# http://www.elasticsearch.org/guide/reference/api/multi-search.html
|
63
|
+
# request may be a hash with the templates names as keys and the variable hash as value
|
64
|
+
# or you can also use an array of arrays.
|
65
|
+
# The variables are an hash of variables that will be used to render the msearch template
|
66
|
+
def multi_search(requests, variables={})
|
67
|
+
lines = requests.map { |name, vars| templates[name].to_msearch(vars) }.join()
|
68
|
+
template = Template.new('GET', '/<<index>>/<<type>>/_msearch')
|
69
|
+
template.send(:do_render, variables.merge(:data => lines)) do |http_response|
|
70
|
+
responses = []
|
71
|
+
es_response = MultiJson.decode(http_response.body)
|
72
|
+
es_response['responses'].each_with_index do |result, i|
|
73
|
+
responses << Result.new(templates[requests[i].first], requests[i].last, http_response, result)
|
74
|
+
end
|
75
|
+
es_response['responses'] = responses
|
76
|
+
def es_response.responses
|
77
|
+
self['responses']
|
78
|
+
end
|
79
|
+
es_response
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def do_load_source(klass, source, source_vars)
|
86
|
+
source = Utils.erb_process(source) unless source.match("\n") # skips non-path
|
87
|
+
hash = Utils.data_from_source(source)
|
88
|
+
hash.delete('ANCHORS')
|
89
|
+
hash.each do |name, structure|
|
90
|
+
if name.to_s =~ /^_/ # partial
|
91
|
+
partial = Template::Partial.new(structure, self)
|
92
|
+
partials[name.to_sym] = partial
|
93
|
+
else
|
94
|
+
template = klass.new(*structure).setup(self, name, source_vars)
|
95
|
+
add_template(name.to_sym, template)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module Flex
|
2
|
-
module
|
3
|
-
class
|
2
|
+
module ClassProxy
|
3
|
+
class Model < Base
|
4
4
|
|
5
|
-
include
|
5
|
+
include ModelSync
|
6
6
|
|
7
7
|
attr_reader :parent_association, :parent_child_map
|
8
8
|
|
9
9
|
def initialize(base)
|
10
10
|
super
|
11
11
|
variables.add :index => Configuration.variables[:index],
|
12
|
-
:type =>
|
12
|
+
:type => ModelManager.class_name_to_type(host_class.name)
|
13
13
|
end
|
14
14
|
|
15
15
|
def index
|
@@ -30,7 +30,7 @@ module Flex
|
|
30
30
|
|
31
31
|
def parent(parent_association, map)
|
32
32
|
@parent_association = parent_association
|
33
|
-
|
33
|
+
ModelManager.parent_types |= map.keys.map(&:to_s)
|
34
34
|
self.type = map.values.map(&:to_s)
|
35
35
|
@parent_child_map = map
|
36
36
|
@is_child = true
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Flex
|
2
|
-
module
|
3
|
-
module
|
2
|
+
module ClassProxy
|
3
|
+
module ModelSync
|
4
4
|
|
5
5
|
def self.included(base)
|
6
6
|
base.class_eval do
|
@@ -12,7 +12,7 @@ module Flex
|
|
12
12
|
@synced = synced
|
13
13
|
host_class.class_eval do
|
14
14
|
raise NotImplementedError, "the class #{self} must implement :after_save and :after_destroy callbacks" \
|
15
|
-
|
15
|
+
unless respond_to?(:after_save) && respond_to?(:after_destroy)
|
16
16
|
after_save { flex.sync }
|
17
17
|
after_destroy { flex.sync }
|
18
18
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Flex
|
2
|
-
module
|
3
|
-
class
|
2
|
+
module ClassProxy
|
3
|
+
class RelatedModel
|
4
4
|
|
5
5
|
attr_reader :host_class
|
6
6
|
|
@@ -8,7 +8,7 @@ module Flex
|
|
8
8
|
@host_class = host_class
|
9
9
|
end
|
10
10
|
|
11
|
-
include
|
11
|
+
include ModelSync
|
12
12
|
|
13
13
|
alias_method :full_sync, :sync
|
14
14
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Flex
|
2
|
-
module
|
3
|
-
class
|
2
|
+
module InstanceProxy
|
3
|
+
class Model < Base
|
4
4
|
|
5
5
|
# indexes the document
|
6
6
|
# usually called from after_save, you can eventually call it explicitly for example from another callback
|
@@ -65,21 +65,21 @@ module Flex
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def is_parent?
|
68
|
-
@is_parent ||=
|
68
|
+
@is_parent ||= ModelManager.parent_types.include?(type)
|
69
69
|
end
|
70
70
|
|
71
71
|
def metainfo
|
72
72
|
@metainfo ||= begin
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
73
|
+
meta = { :index => index, :type => type, :id => id }
|
74
|
+
params = {}
|
75
|
+
params[:routing] = routing if routing
|
76
|
+
params[:parent] = parent_instance.id if is_child?
|
77
|
+
meta.merge!(:params => params) unless params.empty?
|
78
|
+
meta
|
79
|
+
end
|
80
80
|
end
|
81
81
|
|
82
|
-
|
82
|
+
private
|
83
83
|
|
84
84
|
BASE62_DIGITS = %w(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z)
|
85
85
|
|
data/lib/flex/loader.rb
CHANGED
@@ -8,109 +8,11 @@ module Flex
|
|
8
8
|
def self.included(base)
|
9
9
|
base.class_eval do
|
10
10
|
Flex::Loader.host_classes |= [base]
|
11
|
-
|
12
|
-
@flex
|
11
|
+
@flex ||= ClassProxy::Loader.new(base)
|
12
|
+
def self.flex; @flex end
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
class ClassProxy < Flex::ClassProxy
|
17
|
-
attr_reader :templates, :partials
|
18
|
-
|
19
|
-
include Template::Info
|
20
|
-
|
21
|
-
def initialize(base)
|
22
|
-
super
|
23
|
-
@sources = []
|
24
|
-
@templates = {}
|
25
|
-
@partials = {}
|
26
|
-
end
|
27
|
-
|
28
|
-
# accepts a path to a file or YAML string
|
29
|
-
def load_source_for(klass, source, source_vars)
|
30
|
-
if source.nil? || source != /\n/
|
31
|
-
paths = [ "#{Configuration.flex_dir}/#{source}.yml",
|
32
|
-
"#{Configuration.flex_dir}/#{Model::Manager.class_name_to_type(host_class.name)}.yml",
|
33
|
-
source ]
|
34
|
-
source = paths.find {|p| File.exist?(p)}
|
35
|
-
end
|
36
|
-
raise ArgumentError, "expected a string (got #{source.inspect})." \
|
37
|
-
unless source.is_a?(String)
|
38
|
-
@sources << [klass, source, source_vars]
|
39
|
-
do_load_source(klass, source, source_vars)
|
40
|
-
end
|
41
|
-
|
42
|
-
# loads a Generic Template source
|
43
|
-
def load_source(source=nil, source_vars=nil)
|
44
|
-
load_source_for(Template, source, source_vars)
|
45
|
-
end
|
46
|
-
|
47
|
-
# loads a Search Template source
|
48
|
-
def load_search_source(source=nil, source_vars=nil)
|
49
|
-
load_source_for(Template::Search, source, source_vars)
|
50
|
-
end
|
51
|
-
|
52
|
-
# loads a SlimSearch Template source
|
53
|
-
def load_slim_search_source(source=nil, source_vars=nil)
|
54
|
-
load_source_for(Template::SlimSearch, source, source_vars)
|
55
|
-
end
|
56
|
-
|
57
|
-
# reloads the sources (useful in the console and used internally)
|
58
|
-
def reload!
|
59
|
-
@sources.each {|k,s,v| do_load_source(k,s,v)}
|
60
|
-
end
|
61
|
-
|
62
|
-
# adds a template instance and defines the template method in the host class
|
63
|
-
def add_template(name, template)
|
64
|
-
templates[name] = template
|
65
|
-
# no define_singleton_method in 1.8.7
|
66
|
-
host_class.instance_eval <<-ruby, __FILE__, __LINE__ + 1
|
67
|
-
def #{name}(vars={})
|
68
|
-
raise ArgumentError, "#{host_class}.#{name} expects a Hash (got \#{vars.inspect})" unless vars.is_a?(Hash)
|
69
|
-
#{host_class.respond_to?(:preprocess_variables) && 'preprocess_variables(vars)'}
|
70
|
-
flex.templates[:#{name}].render(vars)
|
71
|
-
end
|
72
|
-
ruby
|
73
|
-
end
|
74
|
-
|
75
|
-
# http://www.elasticsearch.org/guide/reference/api/multi-search.html
|
76
|
-
# request may be a hash with the templates names as keys and the variable hash as value
|
77
|
-
# or you can also use an array of arrays.
|
78
|
-
# The variables are an hash of variables that will be used to render the msearch template
|
79
|
-
def multi_search(requests, variables={})
|
80
|
-
lines = requests.map { |name, vars| templates[name].to_msearch(vars) }.join()
|
81
|
-
template = Template.new('GET', '/<<index>>/<<type>>/_msearch')
|
82
|
-
template.send(:do_render, variables.merge(:data => lines)) do |http_response|
|
83
|
-
responses = []
|
84
|
-
es_response = MultiJson.decode(http_response.body)
|
85
|
-
es_response['responses'].each_with_index do |result, i|
|
86
|
-
responses << Result.new(templates[requests[i].first], requests[i].last, http_response, result)
|
87
|
-
end
|
88
|
-
es_response['responses'] = responses
|
89
|
-
def es_response.responses
|
90
|
-
self['responses']
|
91
|
-
end
|
92
|
-
es_response
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
private
|
97
|
-
|
98
|
-
def do_load_source(klass, source, source_vars)
|
99
|
-
source = Utils.erb_process(source) unless source.match("\n") # skips non-path
|
100
|
-
hash = Utils.data_from_source(source)
|
101
|
-
hash.delete('ANCHORS')
|
102
|
-
hash.each do |name, structure|
|
103
|
-
if name.to_s =~ /^_/ # partial
|
104
|
-
partial = Template::Partial.new(structure, self)
|
105
|
-
partials[name.to_sym] = partial
|
106
|
-
else
|
107
|
-
template = klass.new(*structure).setup(self, name, source_vars)
|
108
|
-
add_template(name.to_sym, template)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
16
|
end
|
115
17
|
end
|
116
18
|
|
data/lib/flex/model.rb
CHANGED
@@ -3,13 +3,13 @@ module Flex
|
|
3
3
|
|
4
4
|
def self.included(base)
|
5
5
|
base.class_eval do
|
6
|
-
|
7
|
-
@flex
|
6
|
+
@flex ||= ClassProxy::Model.new(base)
|
7
|
+
def self.flex; @flex end
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
def flex
|
12
|
-
@flex ||= InstanceProxy.new(self)
|
12
|
+
@flex ||= InstanceProxy::Model.new(self)
|
13
13
|
end
|
14
14
|
|
15
15
|
def flex_source
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Flex
|
2
|
+
module ModelManager
|
3
|
+
|
4
|
+
extend self
|
5
|
+
|
6
|
+
attr_accessor :parent_types
|
7
|
+
@parent_types = []
|
8
|
+
|
9
|
+
def init
|
10
|
+
Configuration.flex_models.each {|m| eval"::#{m}" if m.is_a?(String) }
|
11
|
+
end
|
12
|
+
|
13
|
+
# arrays of all the types
|
14
|
+
def types
|
15
|
+
type_class_map.keys.map{|k| k.split('/').last}
|
16
|
+
end
|
17
|
+
|
18
|
+
# sets the default parent/child mappings and merges with the config_file
|
19
|
+
# returns the indices structure used for creating the indices
|
20
|
+
def indices(file=Configuration.config_file)
|
21
|
+
@indices ||= begin
|
22
|
+
default = {}.extend Structure::Mergeable
|
23
|
+
Configuration.flex_models.each do |m|
|
24
|
+
m = eval"::#{m}" if m.is_a?(String)
|
25
|
+
next unless m.flex.is_child?
|
26
|
+
index = m.flex.index
|
27
|
+
m.flex.parent_child_map.each do |parent, child|
|
28
|
+
default.add index => {'mappings' => {child => {'_parent' => {'type' => parent }}}}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
hash = YAML.load(Utils.erb_process(file))
|
32
|
+
hash.delete('ANCHORS')
|
33
|
+
default.deep_merge(hash)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# maps all the index/types to the ruby class
|
38
|
+
def type_class_map
|
39
|
+
@type_class_map ||= begin
|
40
|
+
map = {}
|
41
|
+
Configuration.flex_models.each do |m|
|
42
|
+
m = eval("::#{m}") if m.is_a?(String)
|
43
|
+
types = m.flex.type.is_a?(Array) ? m.flex.type : [m.flex.type]
|
44
|
+
types.each do |t|
|
45
|
+
map["#{m.flex.index}/#{t}"] = m
|
46
|
+
end
|
47
|
+
end
|
48
|
+
map
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def class_name_to_type(class_name)
|
53
|
+
type = class_name.tr(':', '_')
|
54
|
+
type.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
55
|
+
type.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
56
|
+
type.downcase!
|
57
|
+
type
|
58
|
+
end
|
59
|
+
|
60
|
+
def type_to_class_name(type)
|
61
|
+
type.gsub(/__(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
data/lib/flex/rails.rb
CHANGED
@@ -2,11 +2,11 @@ require 'flex'
|
|
2
2
|
require 'rails'
|
3
3
|
require 'flex/rails/helper'
|
4
4
|
|
5
|
-
if Rails.version.
|
5
|
+
if ::Rails.respond_to?(:version) && ::Rails.version.to_i == 3
|
6
6
|
require 'flex/rails/engine'
|
7
7
|
else
|
8
8
|
Flex::Configuration.configure do |c|
|
9
|
-
c.config_file = Rails.root.join('config', 'flex.yml').to_s
|
10
|
-
c.flex_dir = Rails.root.join('app', 'flex').to_s
|
9
|
+
c.config_file = ::Rails.root.join('config', 'flex.yml').to_s
|
10
|
+
c.flex_dir = ::Rails.root.join('app', 'flex').to_s
|
11
11
|
end
|
12
12
|
end
|
data/lib/flex/rails/helper.rb
CHANGED
data/lib/flex/related_model.rb
CHANGED
@@ -3,13 +3,13 @@ module Flex
|
|
3
3
|
|
4
4
|
def self.included(base)
|
5
5
|
base.class_eval do
|
6
|
-
|
7
|
-
@flex
|
6
|
+
@flex ||= ClassProxy::RelatedModel.new(base)
|
7
|
+
def self.flex; @flex end
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
def flex
|
12
|
-
@flex ||= InstanceProxy.new(self)
|
12
|
+
@flex ||= InstanceProxy::RelatedModel.new(self)
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
data/lib/flex/result/document.rb
CHANGED
@@ -23,7 +23,7 @@ module Flex
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def mapped_class(should_raise=false)
|
26
|
-
@mapped_class ||=
|
26
|
+
@mapped_class ||= ModelManager.type_class_map["#{_index}/#{_type}"]
|
27
27
|
rescue NameError
|
28
28
|
raise DocumentMappingError, "the '#{_index}/#{_type}' document cannot be mapped to any class." \
|
29
29
|
if should_raise
|
data/lib/flex/tasks.rb
CHANGED
@@ -110,7 +110,7 @@ module Flex
|
|
110
110
|
'Please, use CONFIG_FILE=/path/to/index.yml ' +
|
111
111
|
'or set the Flex::Configuration.config_file properly' \
|
112
112
|
unless File.exist?(@indices_yaml)
|
113
|
-
|
113
|
+
ModelManager.indices(@indices_yaml)
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
data/lib/flex/template.rb
CHANGED
@@ -105,7 +105,7 @@ module Flex
|
|
105
105
|
|
106
106
|
def caller_line
|
107
107
|
method_name = @host_flex && @name && "#{@host_flex.host_class}.#@name"
|
108
|
-
line = caller.find{|l| l !~
|
108
|
+
line = caller.find{|l| l !~ /#{LIB_PATH}/}
|
109
109
|
ll = ''
|
110
110
|
ll << "#{method_name} from " if method_name
|
111
111
|
ll << "#{line}"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07
|
12
|
+
date: 2012-08-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: multi_json
|
@@ -125,24 +125,26 @@ files:
|
|
125
125
|
- flex.gemspec
|
126
126
|
- lib/flex.rb
|
127
127
|
- lib/flex/api_methods.yml
|
128
|
-
- lib/flex/class_proxy.rb
|
128
|
+
- lib/flex/class_proxy/base.rb
|
129
|
+
- lib/flex/class_proxy/loader.rb
|
130
|
+
- lib/flex/class_proxy/model.rb
|
131
|
+
- lib/flex/class_proxy/model_sync.rb
|
132
|
+
- lib/flex/class_proxy/related_model.rb
|
129
133
|
- lib/flex/configuration.rb
|
130
134
|
- lib/flex/errors.rb
|
131
135
|
- lib/flex/http_clients/patron.rb
|
132
136
|
- lib/flex/http_clients/rest_client.rb
|
137
|
+
- lib/flex/instance_proxy/base.rb
|
138
|
+
- lib/flex/instance_proxy/model.rb
|
139
|
+
- lib/flex/instance_proxy/related_model.rb
|
133
140
|
- lib/flex/loader.rb
|
134
141
|
- lib/flex/logger.rb
|
135
142
|
- lib/flex/model.rb
|
136
|
-
- lib/flex/
|
137
|
-
- lib/flex/model/instance_proxy.rb
|
138
|
-
- lib/flex/model/manager.rb
|
143
|
+
- lib/flex/model_manager.rb
|
139
144
|
- lib/flex/rails.rb
|
140
145
|
- lib/flex/rails/engine.rb
|
141
146
|
- lib/flex/rails/helper.rb
|
142
147
|
- lib/flex/related_model.rb
|
143
|
-
- lib/flex/related_model/class_proxy.rb
|
144
|
-
- lib/flex/related_model/class_sync.rb
|
145
|
-
- lib/flex/related_model/instance_proxy.rb
|
146
148
|
- lib/flex/result.rb
|
147
149
|
- lib/flex/result/bulk.rb
|
148
150
|
- lib/flex/result/collection.rb
|
data/lib/flex/class_proxy.rb
DELETED
data/lib/flex/model/manager.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
module Flex
|
2
|
-
module Model
|
3
|
-
module Manager
|
4
|
-
|
5
|
-
extend self
|
6
|
-
|
7
|
-
attr_accessor :parent_types
|
8
|
-
@parent_types = []
|
9
|
-
|
10
|
-
def init
|
11
|
-
Configuration.flex_models.each {|m| eval"::#{m}" if m.is_a?(String) }
|
12
|
-
end
|
13
|
-
|
14
|
-
# arrays of all the types
|
15
|
-
def types
|
16
|
-
type_class_map.keys.map{|k| k.split('/').last}
|
17
|
-
end
|
18
|
-
|
19
|
-
# sets the default parent/child mappings and merges with the config_file
|
20
|
-
# returns the indices structure used for creating the indices
|
21
|
-
def indices(file=Configuration.config_file)
|
22
|
-
@indices ||= begin
|
23
|
-
default = {}.extend Structure::Mergeable
|
24
|
-
Configuration.flex_models.each do |m|
|
25
|
-
m = eval"::#{m}" if m.is_a?(String)
|
26
|
-
next unless m.flex.is_child?
|
27
|
-
index = m.flex.index
|
28
|
-
m.flex.parent_child_map.each do |parent, child|
|
29
|
-
default.add index => {'mappings' => {child => {'_parent' => {'type' => parent }}}}
|
30
|
-
end
|
31
|
-
end
|
32
|
-
hash = YAML.load(Utils.erb_process(file))
|
33
|
-
hash.delete('ANCHORS')
|
34
|
-
default.deep_merge(hash)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# maps all the index/types to the ruby class
|
39
|
-
def type_class_map
|
40
|
-
@type_class_map ||= begin
|
41
|
-
map = {}
|
42
|
-
Configuration.flex_models.each do |m|
|
43
|
-
m = eval("::#{m}") if m.is_a?(String)
|
44
|
-
types = m.flex.type.is_a?(Array) ? m.flex.type : [m.flex.type]
|
45
|
-
types.each do |t|
|
46
|
-
map["#{m.flex.index}/#{t}"] = m
|
47
|
-
end
|
48
|
-
end
|
49
|
-
map
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def class_name_to_type(class_name)
|
54
|
-
type = class_name.tr(':', '_')
|
55
|
-
type.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
56
|
-
type.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
57
|
-
type.downcase!
|
58
|
-
type
|
59
|
-
end
|
60
|
-
|
61
|
-
def type_to_class_name(type)
|
62
|
-
type.gsub(/__(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|