flex 0.2.1 → 0.3.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 CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/lib/flex.rb CHANGED
@@ -27,7 +27,7 @@ require 'flex/template/slim_search'
27
27
  require 'flex/template/tags'
28
28
  require 'flex/template/info'
29
29
 
30
- require 'flex/model_manager'
30
+ require 'flex/manager'
31
31
 
32
32
  require 'flex/class_proxy/base'
33
33
  require 'flex/class_proxy/loader'
@@ -1,4 +1,4 @@
1
- # These methods are available as Flex.<method> variable_hash
1
+ # These methods are available as Flex.<method>(variable_hash)
2
2
  # you can get the updated full reference and usage example of these methods
3
3
  # by just doing in the console:
4
4
  # >> puts Flex.info
@@ -87,7 +87,7 @@ store: &store
87
87
  # alias for symmetry with post_store
88
88
  put_store: *store
89
89
 
90
- # id is set by ES; you must pass :data
90
+ # id is assigned by ES; you must pass :data
91
91
  post_store:
92
92
  - POST
93
93
  - /<<index>>/<<type>>
@@ -16,7 +16,7 @@ module Flex
16
16
  def load_source_for(klass, source, source_vars)
17
17
  if source.nil? || source != /\n/
18
18
  paths = [ "#{Configuration.flex_dir}/#{source}.yml",
19
- "#{Configuration.flex_dir}/#{ModelManager.class_name_to_type(host_class.name)}.yml",
19
+ "#{Configuration.flex_dir}/#{Manager.class_name_to_type(host_class.name)}.yml",
20
20
  source.to_s ]
21
21
  source = paths.find {|p| File.exist?(p)}
22
22
  end
@@ -9,7 +9,7 @@ module Flex
9
9
  def initialize(base)
10
10
  super
11
11
  variables.add :index => Configuration.variables[:index],
12
- :type => ModelManager.class_name_to_type(host_class.name)
12
+ :type => Manager.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
- ModelManager.parent_types |= map.keys.map(&:to_s)
33
+ Manager.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
@@ -3,8 +3,8 @@ module Flex
3
3
  module Patron
4
4
  extend self
5
5
 
6
- def request(method, path, data=nil, options={})
7
- options = Utils.deep_merge_hashes(Configuration.http_client_options, options)
6
+ def request(method, path, data=nil)
7
+ options = Configuration.http_client_options
8
8
  options = options.merge(:data => data) if data
9
9
  session.request method.to_s.downcase.to_sym, path, {}, options
10
10
  rescue ::Patron::TimeoutError
@@ -3,9 +3,9 @@ module Flex
3
3
  module RestClient
4
4
  extend self
5
5
 
6
- def request(method, path, data=nil, options={})
7
- options = Configuration.http_client_options.merge options
8
- url = Configuration.base_uri + path
6
+ def request(method, path, data=nil)
7
+ options = Configuration.http_client_options
8
+ url = Configuration.base_uri.join(path)
9
9
  args = options.merge( :method => method.to_s.downcase.to_sym,
10
10
  :url => url,
11
11
  :payload => data )
@@ -65,7 +65,7 @@ module Flex
65
65
  end
66
66
 
67
67
  def is_parent?
68
- @is_parent ||= ModelManager.parent_types.include?(type)
68
+ @is_parent ||= Manager.parent_types.include?(type)
69
69
  end
70
70
 
71
71
  def metainfo
@@ -0,0 +1,61 @@
1
+ module Flex
2
+ module Manager
3
+
4
+ extend self
5
+
6
+ attr_accessor :parent_types
7
+ @parent_types = []
8
+
9
+ def init_models
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 ||= ( default = {}.extend Structure::Mergeable
22
+ Configuration.flex_models.each do |m|
23
+ m = eval"::#{m}" if m.is_a?(String)
24
+ next unless m.flex.is_child?
25
+ index = m.flex.index
26
+ m.flex.parent_child_map.each do |parent, child|
27
+ default.add index => {'mappings' => {child => {'_parent' => {'type' => parent }}}}
28
+ end
29
+ end
30
+ hash = YAML.load(Utils.erb_process(file))
31
+ hash.delete('ANCHORS')
32
+ default.deep_merge(hash) )
33
+ end
34
+
35
+ # maps all the index/types to the ruby class
36
+ def type_class_map
37
+ @type_class_map ||= ( map = {}
38
+ Configuration.flex_models.each do |m|
39
+ m = eval("::#{m}") if m.is_a?(String)
40
+ types = m.flex.type.is_a?(Array) ? m.flex.type : [m.flex.type]
41
+ types.each do |t|
42
+ map["#{m.flex.index}/#{t}"] = m
43
+ end
44
+ end
45
+ map )
46
+ end
47
+
48
+ def class_name_to_type(class_name)
49
+ type = class_name.tr(':', '_')
50
+ type.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
51
+ type.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
52
+ type.downcase!
53
+ type
54
+ end
55
+
56
+ def type_to_class_name(type)
57
+ type.gsub(/__(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
58
+ end
59
+
60
+ end
61
+ end
@@ -8,7 +8,7 @@ module Flex
8
8
  Flex::Configuration.logger = ::Rails.logger
9
9
  # we need to reload the flex API methods with the new variables
10
10
  Flex.reload!
11
- Flex::ModelManager.init
11
+ Flex::Manager.init_models
12
12
  end
13
13
 
14
14
  end
@@ -23,7 +23,7 @@ module Flex
23
23
  end
24
24
 
25
25
  def mapped_class(should_raise=false)
26
- @mapped_class ||= ModelManager.type_class_map["#{_index}/#{_type}"]
26
+ @mapped_class ||= Manager.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
@@ -93,7 +93,7 @@ module Flex
93
93
  private
94
94
 
95
95
  def indices
96
- indices = ENV['INDEX'] || ENV['INDICES'] || struct.keys
96
+ indices = ENV['INDICES'] || struct.keys
97
97
  indices = eval(indices) if indices.is_a?(String)
98
98
  indices = [indices] unless indices.is_a?(Array)
99
99
  indices
@@ -110,15 +110,15 @@ 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
- ModelManager.indices(@indices_yaml)
113
+ Manager.indices(@indices_yaml)
114
114
  end
115
115
  end
116
116
 
117
117
 
118
118
  def models
119
119
  @models ||= begin
120
- mods = ENV['MODEL'] || ENV['MODELS'] || Flex::Configuration.flex_models
121
- raise AgrumentError, 'no class defined. Please use MODEL=AClass or MODELS=[ClassA,ClassB]' +
120
+ mods = ENV['MODELS'] || Flex::Configuration.flex_models
121
+ raise AgrumentError, 'no class defined. Please use MODELS=[ClassA,ClassB]' +
122
122
  'or set the Flex::Configuration.flex_models properly' \
123
123
  if mods.nil? || mods.empty?
124
124
  mods = eval(mods) if mods.is_a?(String)
@@ -1,11 +1,6 @@
1
1
  module Flex
2
2
  module UtilityMethods
3
3
 
4
- def configuration
5
- Configuration
6
- end
7
- alias_method :config, :configuration
8
-
9
4
  # Anonymous search query: please, consider to use named templates for better performances and programming style
10
5
  # data can be a JSON string that will be passed as is, or a YAML string (that will be converted into a ruby hash)
11
6
  # or a hash. It can contain interpolation tags as usual.
@@ -2,9 +2,6 @@ require 'prompter'
2
2
 
3
3
  class Flex::SetupGenerator < Rails::Generators::Base
4
4
 
5
- # more funny than vanilla thor
6
- include Prompter::Methods
7
-
8
5
  source_root File.expand_path('../templates', __FILE__)
9
6
 
10
7
  def self.banner
@@ -12,8 +9,8 @@ class Flex::SetupGenerator < Rails::Generators::Base
12
9
  end
13
10
 
14
11
  def ask_base_name
15
- @class_name = ask('Please, enter a class name for your Search class. Choose a name not defined in your app.',
16
- :default => 'FlexSearch', :hint => '[<enter>=FlexSearch]')
12
+ @class_name = Prompter.ask('Please, enter a class name for your Search class. Choose a name not defined in your app.',
13
+ :default => 'FlexSearch', :hint => '[<enter>=FlexSearch]')
17
14
  @extender_name = "#{@class_name}Extender"
18
15
  end
19
16
 
data/lib/tasks/index.rake CHANGED
@@ -9,15 +9,9 @@ namespace :flex do
9
9
  task(:import => env) { Flex::Tasks.import_models }
10
10
 
11
11
  desc 'create indices from the Flex::Configuration.config_file file'
12
- task(:create_index => env) { Flex::Tasks.create_indices }
13
-
14
- desc 'alias for flex:create_index'
15
12
  task(:create_indices => env) { Flex::Tasks.create_indices }
16
13
 
17
14
  desc 'destroy indices in the Flex::Configuration.config_file file'
18
- task(:delete_index => env) { Flex::Tasks.delete_indices }
19
-
20
- desc 'alias for flex:delete_index'
21
15
  task(:delete_indices => env) { Flex::Tasks.delete_indices }
22
16
 
23
17
  end
data/test/flex.irt CHANGED
@@ -7,7 +7,7 @@ methods = [ # api methods from yaml
7
7
  :indices_exists, :create_index, :put_index, :post_index, :get_settings, :put_mapping, :get_mapping, :delete_mapping,
8
8
  :delete_index, :delete_by_query, :bulk, :count, :stats, :store, :put_store, :post_store, :remove, :get, :multi_get,
9
9
  # utility_methods
10
- :configuration, :config, :search, :slim_search, :HEAD, :GET, :PUT, :POST, :DELETE, :json2yaml, :yaml2json,
10
+ :search, :slim_search, :HEAD, :GET, :PUT, :POST, :DELETE, :json2yaml, :yaml2json,
11
11
  :process_bulk, :import_collection, :delete_collection,
12
12
  # other methods
13
13
  :exist? ]
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.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-15 00:00:00.000000000 Z
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70336369278160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: 1.3.4
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 1.3.4
24
+ version_requirements: *70336369278160
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: progressbar
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &70336369277440 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ~>
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: 0.11.0
38
33
  type: :runtime
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 0.11.0
35
+ version_requirements: *70336369277440
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: prompter
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &70336369276780 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ~>
@@ -53,15 +43,10 @@ dependencies:
53
43
  version: 0.1.5
54
44
  type: :runtime
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: 0.1.5
46
+ version_requirements: *70336369276780
62
47
  - !ruby/object:Gem::Dependency
63
48
  name: irt
64
- requirement: !ruby/object:Gem::Requirement
49
+ requirement: &70336369276080 !ruby/object:Gem::Requirement
65
50
  none: false
66
51
  requirements:
67
52
  - - ~>
@@ -69,15 +54,10 @@ dependencies:
69
54
  version: 1.2.10
70
55
  type: :development
71
56
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: 1.2.10
57
+ version_requirements: *70336369276080
78
58
  - !ruby/object:Gem::Dependency
79
59
  name: patron
80
- requirement: !ruby/object:Gem::Requirement
60
+ requirement: &70336369275480 !ruby/object:Gem::Requirement
81
61
  none: false
82
62
  requirements:
83
63
  - - ~>
@@ -85,15 +65,10 @@ dependencies:
85
65
  version: 0.4.18
86
66
  type: :development
87
67
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ~>
92
- - !ruby/object:Gem::Version
93
- version: 0.4.18
68
+ version_requirements: *70336369275480
94
69
  - !ruby/object:Gem::Dependency
95
70
  name: rest-client
96
- requirement: !ruby/object:Gem::Requirement
71
+ requirement: &70336369275020 !ruby/object:Gem::Requirement
97
72
  none: false
98
73
  requirements:
99
74
  - - ~>
@@ -101,12 +76,7 @@ dependencies:
101
76
  version: 1.6.7
102
77
  type: :development
103
78
  prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ~>
108
- - !ruby/object:Gem::Version
109
- version: 1.6.7
79
+ version_requirements: *70336369275020
110
80
  description: ! 'Flex is an innovative ruby client for ElasticSearch. With Flex your
111
81
  code will be clean, easy to write and read, and very short: "poetic-short". Flex
112
82
  is fast and efficient, easy to use and customize, and offers ActiveRecord, MongoId
@@ -139,8 +109,8 @@ files:
139
109
  - lib/flex/instance_proxy/related_model.rb
140
110
  - lib/flex/loader.rb
141
111
  - lib/flex/logger.rb
112
+ - lib/flex/manager.rb
142
113
  - lib/flex/model.rb
143
- - lib/flex/model_manager.rb
144
114
  - lib/flex/rails.rb
145
115
  - lib/flex/rails/engine.rb
146
116
  - lib/flex/rails/helper.rb
@@ -204,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
204
174
  version: 1.3.6
205
175
  requirements: []
206
176
  rubyforge_project:
207
- rubygems_version: 1.8.24
177
+ rubygems_version: 1.8.10
208
178
  signing_key:
209
179
  specification_version: 3
210
180
  summary: Ruby Client for ElasticSearch
@@ -1,65 +0,0 @@
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