dilute 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ dilute
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p374
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dilute.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sam Schenkman-Moore
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Dilute
2
+
3
+ It's an ORM for ElasticSearch.
4
+
5
+ Disclosure: I'm learning ElasticSearch, this is the product of some of my experiments. This has been helpful for my needs, but is very new.
6
+
7
+ ## Installation
8
+
9
+ gem 'dilute'
10
+
11
+ ## Usage
12
+
13
+ In a Rails app called "MyRailsApp" ...
14
+
15
+ class Note
16
+ include Dilute::Modelize
17
+
18
+ define_type do
19
+ attribute(:data).not_indexed
20
+ attribute(:header).not_indexed
21
+ attribute(:keys).index_analyzer("keyword")
22
+ attribute(:created_at, :date)
23
+ end
24
+ end
25
+
26
+ This will create a new index called "MyRailsApp" with a new type mapping "notes".
27
+
28
+ define_type can some options: `server_url`, `index_name`, `type_name`
29
+
30
+ define_type(server_url: "http://example.com/", index_name: "special_name", type_name: "something_else")
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/dilute.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dilute/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dilute"
8
+ spec.version = Dilute::VERSION
9
+ spec.authors = ["Sam Schenkman-Moore"]
10
+ spec.email = ["samsm@samsm.com"]
11
+ spec.description = %q{A basic ORM-sort of thing for Stretcher.}
12
+ spec.summary = %q{An ActiveRecord-ish thing for using ElasticSearch through Stretcher.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "stretcher"
22
+ spec.add_dependency "activesupport"
23
+ spec.add_dependency "activemodel"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "pry"
29
+ end
@@ -0,0 +1,54 @@
1
+ require "active_support/hash_with_indifferent_access"
2
+
3
+ class Dilute::Attribute
4
+ attr_reader :options
5
+ def initialize(options = {})
6
+ @options = HashWithIndifferentAccess.new.merge(options)
7
+ end
8
+
9
+ %w(index_name store index term_vector boost null_value omit_norms
10
+ omit_term_freq_and_positions index_options analyzer index_analyzer
11
+ search_analyzer include_in_all ignore_above position_offset_gap
12
+ precision_step ignore_malformed format).each do |option|
13
+ define_method option do |new_value|
14
+ raise "invalid option #{option} for #{type}" unless acceptable_mapping_options[type_mapping].include?(option)
15
+ chain_add(option => new_value)
16
+ end
17
+ end
18
+
19
+ def not_indexed
20
+ index("not_analyzed")
21
+ end
22
+
23
+ def to_config
24
+ options
25
+ end
26
+
27
+ private
28
+
29
+ def chain_add(hsh)
30
+ # self.class.new(options.merge(hsh))
31
+ options.merge!(hsh)
32
+ self
33
+ end
34
+
35
+ def type(new_val = nil)
36
+ new_val ? options[:type] = new_val.to_sym : options[:type].to_sym
37
+ end
38
+
39
+ def type_mapping
40
+ return :number if [:float, :double, :integer, :long, :short, :byte].include?(type)
41
+ type
42
+ end
43
+
44
+
45
+ def acceptable_mapping_options
46
+ {
47
+ string: %w(index_name store index term_vector boost null_value omit_norms omit_term_freq_and_positions index_options analyzer index_analyzer search_analyzer include_in_all ignore_above position_offset_gap),
48
+ number: %w(type index_name store index precision_step boost null_value include_in_all ignore_malformed),
49
+ date: %w(index_name format store index precision_step boost null_value include_in_all ignore_malformed),
50
+ boolean: %w(index_name store index boost null_value include_in_all),
51
+ binary: %w(index_name)
52
+ }
53
+ end
54
+ end
@@ -0,0 +1,103 @@
1
+ require "active_support/concern"
2
+ require "active_model/naming"
3
+ require 'active_support/core_ext/string'
4
+
5
+ module Dilute::Modelize
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ attr_reader :attributes
10
+ extend ActiveModel::Naming
11
+ end
12
+
13
+ module ClassMethods
14
+ def name
15
+ super || "DefaultDilutedModel"
16
+ end
17
+
18
+ def find(id)
19
+ new(type.get(id, {}, true))
20
+ end
21
+
22
+ def all(query = {}, page_size = 10)
23
+ search_results = type.search(size: page_size, query: {match_all: {}})
24
+ search_results.raw["hits"]["hits"].collect {|r| new(r) }
25
+ end
26
+
27
+ def define_type(options = {}, &blk)
28
+ @type ||= Dilute::Type.new(type_defaults.merge(options), &blk)
29
+ add_attribute_accessors
30
+ end
31
+
32
+ def application_name
33
+ if Kernel.const_defined? :Rails
34
+ Rails.application.class.parent_name
35
+ else
36
+ "default_dilute_index_name"
37
+ end
38
+ end
39
+
40
+ def type_defaults
41
+ HashWithIndifferentAccess.new({
42
+ index_name: application_name,
43
+ type_name: model_name.plural
44
+ })
45
+ end
46
+
47
+ def add_attribute_accessors
48
+ type.properties.keys.each do |k|
49
+ define_method k do
50
+ attributes["_source"] && attributes["_source"][k]
51
+ end
52
+ define_method "#{k}=" do |new_var|
53
+ attributes["_source"] && attributes["_source"][k] = new_var
54
+ end
55
+ end
56
+ end
57
+
58
+ def type
59
+ @type or raise "No type defined!"
60
+ end
61
+ end
62
+
63
+ def id
64
+ attributes["_id"]
65
+ end
66
+
67
+ def attributes_without_elasticsearch_vars
68
+ # elasticsearch_vars = %w(ok _index _type _id _version)
69
+ attributes["_source"]
70
+ end
71
+
72
+ def save
73
+ if id
74
+ type.put(id, attributes_without_elasticsearch_vars)
75
+ else
76
+ results = type.post(attributes_without_elasticsearch_vars)
77
+ if results["ok"] == true
78
+ attributes.merge!(results)
79
+ else
80
+ puts "Something went wrong!"
81
+ puts "attributes: #{attributes}"
82
+ end
83
+ end
84
+ end
85
+
86
+ def destroy
87
+ raise "id can't be nil" unless id
88
+ type.delete(id)
89
+ end
90
+
91
+ def initialize(attributes = {})
92
+ @attributes = if attributes.has_key?("_source")
93
+ attributes
94
+ else
95
+ {"_source" => HashWithIndifferentAccess.new.merge(attributes)}
96
+ end
97
+ end
98
+
99
+ def type
100
+ self.class.type.type
101
+ end
102
+
103
+ end
@@ -0,0 +1,75 @@
1
+ require "active_support/hash_with_indifferent_access"
2
+ require "stretcher"
3
+
4
+ class Dilute::Type
5
+ attr_reader :options, :attributes
6
+ def initialize(options = {}, &block)
7
+ @options = HashWithIndifferentAccess.new.merge(options)
8
+ @attributes = HashWithIndifferentAccess.new
9
+ instance_eval(&block) if block_given?
10
+ end
11
+
12
+ def server_url
13
+ options[:server_url] || "http://localhost:9200"
14
+ end
15
+
16
+ def server
17
+ @server ||= Stretcher::Server.new(server_url, log_level: :info)
18
+ end
19
+
20
+ def index_name
21
+ options[:index_name] # :qknt
22
+ end
23
+
24
+ def type_name
25
+ options[:type_name] # :notes
26
+ end
27
+
28
+ def index
29
+ @index ||= begin
30
+ the_index = server.index(index_name)
31
+ the_index.exists? ? the_index : (the_index.create && the_index)
32
+ end
33
+ end
34
+
35
+ def type
36
+ @type ||= begin
37
+ the_type = index.type(type_name)
38
+ the_type.exists? ? the_type : (the_type.put_mapping(generate_mapping) && the_type)
39
+ end
40
+ end
41
+
42
+ def delete
43
+ result = type.delete_mapping
44
+ @type = nil
45
+ result
46
+ end
47
+
48
+ def generate_mapping
49
+ { :"#{type_name}" => { properties: properties } }
50
+ end
51
+
52
+ def properties
53
+ attributes.inject({}) do |sum, pair|
54
+ k, v = pair
55
+ sum[k] = v.to_config ; sum
56
+ end
57
+ end
58
+
59
+ def mapping
60
+ type.get_mapping
61
+ end
62
+
63
+ def attribute(key, attribute_type = :string)
64
+ attributes[key] = Dilute::Attribute.new(type: attribute_type)
65
+ end
66
+
67
+ def search(*args)
68
+ type.search *args
69
+ end
70
+
71
+ def get(*args)
72
+ type.get *args
73
+ end
74
+
75
+ end
@@ -0,0 +1,3 @@
1
+ module Dilute
2
+ VERSION = "0.0.1"
3
+ end
data/lib/dilute.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Dilute
2
+
3
+ # autoload all the stuff in filters under this namespace
4
+ Dir.glob("#{File.dirname(__FILE__)}/dilute/*.rb").each do |filename|
5
+ class_name = File.basename(filename, '.*').split('_').collect(&:capitalize).join.to_sym
6
+ autoload class_name, filename
7
+ end
8
+
9
+ end
@@ -0,0 +1,47 @@
1
+ require_relative "spec_helper"
2
+ require "pry"
3
+
4
+ describe Dilute::Modelize do
5
+ let(:dilute_class) do
6
+ Class.new do
7
+ include Dilute::Modelize
8
+
9
+ define_type(index_name: :dilute_test_suite, type_name: :abstract) do
10
+ attribute(:data).not_indexed
11
+ attribute(:header).not_indexed
12
+ attribute(:keys).index_analyzer("keyword")
13
+ attribute(:created_at, :date)
14
+ end
15
+ end
16
+ end
17
+
18
+ let(:test_data) { "foo city" }
19
+ let(:new_diluted) { dilute_class.new({data: test_data}) }
20
+ let(:saved) { new_diluted.save ; new_diluted }
21
+
22
+ it "should have accessors for new object" do
23
+ expect(new_diluted.data).to eq(test_data)
24
+ end
25
+
26
+ it "should save data" do
27
+ expect(new_diluted.id).to be_nil
28
+ new_diluted.save
29
+ expect(new_diluted.id).not_to be_nil
30
+ end
31
+
32
+ it "should retrieve saved data" do
33
+ expect(dilute_class.find(saved.id).data).to eq(test_data)
34
+ end
35
+
36
+ it "should retreive many saved documents" do
37
+ destroy_test_index!
38
+ 10.times { dilute_class.new({data: test_data}).save }
39
+ sleep 1
40
+ expect(dilute_class.all({}, 5)).to have(5).items
41
+ end
42
+
43
+ it "I have no idea what I'm doing." do
44
+ true
45
+ end
46
+
47
+ end
@@ -0,0 +1,26 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ # config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ require "dilute"
20
+
21
+ def destroy_test_index!
22
+ require "stretcher"
23
+ server = Stretcher::Server.new("http://localhost:9200", log_level: :info)
24
+ index = server.index("dilute_test_suite")
25
+ index.delete
26
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dilute
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Schenkman-Moore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: stretcher
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activemodel
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.3'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.3'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: pry
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: A basic ORM-sort of thing for Stretcher.
127
+ email:
128
+ - samsm@samsm.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - .ruby-gemset
136
+ - .ruby-version
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - dilute.gemspec
142
+ - lib/dilute.rb
143
+ - lib/dilute/attribute.rb
144
+ - lib/dilute/modelize.rb
145
+ - lib/dilute/type.rb
146
+ - lib/dilute/version.rb
147
+ - spec/modelize_integration_spec.rb
148
+ - spec/spec_helper.rb
149
+ homepage: ''
150
+ licenses:
151
+ - MIT
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ segments:
163
+ - 0
164
+ hash: -1974874539562345975
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ segments:
172
+ - 0
173
+ hash: -1974874539562345975
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 1.8.25
177
+ signing_key:
178
+ specification_version: 3
179
+ summary: An ActiveRecord-ish thing for using ElasticSearch through Stretcher.
180
+ test_files:
181
+ - spec/modelize_integration_spec.rb
182
+ - spec/spec_helper.rb