locomotivecms_models 0.0.1.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +53 -0
- data/README.md +29 -0
- data/Rakefile +12 -0
- data/example.rb +76 -0
- data/lib/locomotive/adapters/memory/command.rb +35 -0
- data/lib/locomotive/adapters/memory/condition.rb +98 -0
- data/lib/locomotive/adapters/memory/dataset.rb +75 -0
- data/lib/locomotive/adapters/memory/query.rb +106 -0
- data/lib/locomotive/adapters/memory/wrapper.rb +23 -0
- data/lib/locomotive/adapters/memory_adapter.rb +72 -0
- data/lib/locomotive/core_ext.rb +2 -0
- data/lib/locomotive/core_ext/hash.rb +44 -0
- data/lib/locomotive/core_ext/string.rb +13 -0
- data/lib/locomotive/decorators.rb +1 -0
- data/lib/locomotive/decorators/i18n_decorator.rb +45 -0
- data/lib/locomotive/entity.rb +28 -0
- data/lib/locomotive/fields/i18n_field.rb +62 -0
- data/lib/locomotive/mapper.rb +47 -0
- data/lib/locomotive/mapping.rb +16 -0
- data/lib/locomotive/mapping/coercer.rb +64 -0
- data/lib/locomotive/mapping/collection.rb +68 -0
- data/lib/locomotive/mapping/dereferencer.rb +40 -0
- data/lib/locomotive/mapping/referencer.rb +66 -0
- data/lib/locomotive/mapping/virtual_proxy.rb +30 -0
- data/lib/locomotive/models.rb +57 -0
- data/lib/locomotive/models/configuration.rb +13 -0
- data/lib/locomotive/models/version.rb +10 -0
- data/lib/locomotive/repository.rb +51 -0
- data/locomotive_models.gemspec +28 -0
- data/non_persisted_example.rb +70 -0
- data/presenter_example.rb +49 -0
- data/relation_example.rb +119 -0
- data/spec/fixtures/example_entities.rb +23 -0
- data/spec/fixtures/example_mapper.rb +40 -0
- data/spec/fixtures/example_repositories.rb +19 -0
- data/spec/integration/criteria_spec.rb +67 -0
- data/spec/integration/persistence_entity_spec.rb +75 -0
- data/spec/integration/relations_spec.rb +114 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/adapters/memory.rb +39 -0
- data/spec/unit/adapters/memory/condition_spec.rb +120 -0
- data/spec/unit/adapters/memory/dataset_spec.rb +71 -0
- data/spec/unit/adapters/memory/query_spec.rb +69 -0
- data/spec/unit/decorators/i18n_decorator_spec.rb +133 -0
- data/spec/unit/entity_spec.rb +30 -0
- data/spec/unit/fields/i18n_field_spec.rb +60 -0
- data/spec/unit/mapper_spec.rb +60 -0
- data/spec/unit/mapping/coercer_spec.rb +30 -0
- data/spec/unit/mapping/collection_spec.rb +32 -0
- data/spec/unit/models_spec.rb +14 -0
- metadata +190 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Adapters
|
3
|
+
module Memory
|
4
|
+
|
5
|
+
class Wrapper < Struct.new(:query, :collection)
|
6
|
+
|
7
|
+
def first
|
8
|
+
all.first
|
9
|
+
end
|
10
|
+
|
11
|
+
def all
|
12
|
+
collection.deserialize(query)
|
13
|
+
end
|
14
|
+
|
15
|
+
def constraints &block
|
16
|
+
query.instance_eval(&block) if block_given?
|
17
|
+
query
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative 'memory/dataset'
|
2
|
+
require_relative 'memory/condition'
|
3
|
+
require_relative 'memory/query'
|
4
|
+
require_relative 'memory/command'
|
5
|
+
require_relative 'memory/wrapper'
|
6
|
+
|
7
|
+
module Locomotive
|
8
|
+
module Adapters
|
9
|
+
|
10
|
+
class MemoryAdapter
|
11
|
+
|
12
|
+
def initialize mapper
|
13
|
+
@mapper = mapper
|
14
|
+
@datasets = Hash.new { |hash, name| hash[name] = Memory::Dataset.new(name) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def all(collection)
|
18
|
+
_mapped_collection(collection).deserialize(dataset(collection).all)
|
19
|
+
end
|
20
|
+
|
21
|
+
def create(collection, entity)
|
22
|
+
Memory::Command.new(dataset(collection), _mapped_collection(collection)).create(entity)
|
23
|
+
end
|
24
|
+
|
25
|
+
def update(collection, entity)
|
26
|
+
Memory::Command.new(dataset(collection), _mapped_collection(collection)).update(entity)
|
27
|
+
end
|
28
|
+
|
29
|
+
def destroy(collection, entity)
|
30
|
+
Memory::Command.new(dataset(collection), collection).destroy(entity)
|
31
|
+
end
|
32
|
+
|
33
|
+
def persisted?(collection, entity)
|
34
|
+
entity.id && dataset(collection).exists?(entity.id)
|
35
|
+
end
|
36
|
+
|
37
|
+
def first(collection)
|
38
|
+
dataset(collection).first
|
39
|
+
end
|
40
|
+
|
41
|
+
def last(collection)
|
42
|
+
dataset(collection).last
|
43
|
+
end
|
44
|
+
|
45
|
+
def size(collection)
|
46
|
+
dataset(collection).size
|
47
|
+
end
|
48
|
+
|
49
|
+
def query(collection, locale=nil, &block)
|
50
|
+
query = Memory::Query.new(dataset(collection), locale, &block)
|
51
|
+
Memory::Wrapper.new query, _mapped_collection(collection)
|
52
|
+
end
|
53
|
+
|
54
|
+
def find(collection, id)
|
55
|
+
record = dataset(collection).find(id)
|
56
|
+
_mapped_collection(collection).deserialize([record]).first
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def dataset(collection)
|
63
|
+
@datasets[collection]
|
64
|
+
end
|
65
|
+
|
66
|
+
def _mapped_collection(name)
|
67
|
+
@mapper.collection(name)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
def has_key_indifferent_access? key
|
4
|
+
self.has_key?(key.to_s) or self.has_key?(key.to_sym)
|
5
|
+
end
|
6
|
+
|
7
|
+
def respond_to? method_name
|
8
|
+
return true if has_key_indifferent_access?(method_name)
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing method_name, *args
|
13
|
+
return self.deep_stringify_keys.fetch(method_name.to_s) if has_key_indifferent_access?(method_name)
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns a new hash with all keys converted by the block operation.
|
18
|
+
# This includes the keys from the root hash and from all
|
19
|
+
# nested hashes.
|
20
|
+
#
|
21
|
+
# hash = { person: { name: 'Rob', age: '28' } }
|
22
|
+
#
|
23
|
+
# hash.deep_transform_keys{ |key| key.to_s.upcase }
|
24
|
+
# # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
|
25
|
+
def deep_transform_keys(&block)
|
26
|
+
result = {}
|
27
|
+
each do |key, value|
|
28
|
+
result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value
|
29
|
+
end
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns a new hash with all keys converted to strings.
|
34
|
+
# This includes the keys from the root hash and from all
|
35
|
+
# nested hashes.
|
36
|
+
#
|
37
|
+
# hash = { person: { name: 'Rob', age: '28' } }
|
38
|
+
#
|
39
|
+
# hash.deep_stringify_keys
|
40
|
+
# # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
|
41
|
+
def deep_stringify_keys
|
42
|
+
deep_transform_keys{ |key| key.to_s }
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class String #:nodoc
|
2
|
+
|
3
|
+
def permalink(underscore = false)
|
4
|
+
_permalink = self.gsub(/[\s]+/, '_')
|
5
|
+
underscore ? _permalink.underscore : _permalink
|
6
|
+
end
|
7
|
+
|
8
|
+
def permalink!(underscore = false)
|
9
|
+
replace(self.permalink(underscore))
|
10
|
+
end
|
11
|
+
|
12
|
+
alias :parameterize! :permalink!
|
13
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'decorators/i18n_decorator'
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Decorators
|
3
|
+
class I18nDecorator < SimpleDelegator
|
4
|
+
|
5
|
+
attr_accessor :current_locale
|
6
|
+
attr_writer :on_no_locale, :on_empty_locale
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def decorate(resultset, locale = nil)
|
10
|
+
resultset.map { |obj| new(obj).tap {|decorated_obj| decorated_obj.current_locale = locale} }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(object, locale = nil)
|
15
|
+
self.current_locale = locale
|
16
|
+
super(object)
|
17
|
+
end
|
18
|
+
|
19
|
+
def current_locale= _locale
|
20
|
+
@current_locale = _locale.try(:to_sym)
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(name, *args, &block)
|
24
|
+
begin
|
25
|
+
__getobj__.public_send(name).to_s(current_locale)
|
26
|
+
rescue Locomotive::Fields::I18nField::NoLocaleError
|
27
|
+
on_no_locale.call __getobj__.send(name), current_locale
|
28
|
+
rescue Locomotive::Fields::I18nField::EmptyLocaleError
|
29
|
+
on_empty_locale.call __getobj__.send(name), current_locale
|
30
|
+
rescue ArgumentError, TypeError
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def on_no_locale
|
36
|
+
@on_no_locale ||= Proc.new { |field, locale| nil }
|
37
|
+
end
|
38
|
+
|
39
|
+
def on_empty_locale
|
40
|
+
@on_empty_locale ||= Proc.new { |field, locale| field.values.first }
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Entity
|
3
|
+
|
4
|
+
attr_accessor :id
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize attributes = {}
|
11
|
+
attributes.each do |key, value|
|
12
|
+
case value
|
13
|
+
when Hash
|
14
|
+
self.send "#{key}=", Locomotive::Fields::I18nField.new(value)
|
15
|
+
else
|
16
|
+
self.send "#{key}=", value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
|
23
|
+
def attributes *args
|
24
|
+
attr_accessor(*args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module Locomotive
|
4
|
+
module Fields
|
5
|
+
class I18nField
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
class UnsupportedFormat < StandardError ; end
|
9
|
+
class NoLocaleError < StandardError ; end
|
10
|
+
class EmptyLocaleError < StandardError ; end
|
11
|
+
|
12
|
+
class I18nValues < Hash
|
13
|
+
def method_missing method_name, *args
|
14
|
+
return self.fetch(method_name) if self.has_key?(method_name)
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_accessor :i18n_values
|
20
|
+
def_delegators :i18n_values, :values, :[], :[]=, :each, :keys
|
21
|
+
|
22
|
+
def initialize i18n_values = nil
|
23
|
+
@i18n_values = I18nValues.new
|
24
|
+
self << i18n_values
|
25
|
+
end
|
26
|
+
|
27
|
+
def << _i18n_values
|
28
|
+
i18n_values && @i18n_values.merge!(_i18n_values.symbolize_keys)
|
29
|
+
|
30
|
+
rescue TypeError
|
31
|
+
raise UnsupportedFormat.new("waiting format: { locale: value } not ''#{i18n_values}''")
|
32
|
+
end
|
33
|
+
|
34
|
+
def remove locale
|
35
|
+
@i18n_values.delete locale
|
36
|
+
end
|
37
|
+
|
38
|
+
def locales
|
39
|
+
@i18n_values.keys
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s locale = nil
|
43
|
+
if locale
|
44
|
+
i18n_values.fetch(locale) do
|
45
|
+
raise NoLocaleError
|
46
|
+
end.tap do |value|
|
47
|
+
raise EmptyLocaleError if value.empty?
|
48
|
+
end
|
49
|
+
else
|
50
|
+
"#<I18nField: @i18n_values=>{" + i18n_values.map{|k,v|":#{k}=>#{v}"}.join(',') + '}>'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
alias_method :inspect, :to_s
|
54
|
+
|
55
|
+
def method_missing method_name, *args
|
56
|
+
return i18n_values.send(method_name) if i18n_values.respond_to?(method_name)
|
57
|
+
super
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Locomotive
|
2
|
+
class Mapper
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def load_from_file!(adapter, file)
|
6
|
+
self.new(adapter) do
|
7
|
+
instance_eval File.read(file), file
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :collections, :adapter
|
13
|
+
|
14
|
+
def initialize(adapter, coercer = nil, &blk)
|
15
|
+
@coercer = coercer || Mapping::Coercer
|
16
|
+
@collections = {}
|
17
|
+
@adapter = adapter.new self
|
18
|
+
|
19
|
+
instance_eval(&blk) if block_given?
|
20
|
+
|
21
|
+
registry! # TODO add protection for not overrride instance vairable
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_association! object, relation, request
|
25
|
+
object.send(:"#{relation}=", Locomotive::Mapping::VirtualProxy.new do
|
26
|
+
object.send(:"#{relation}=", request)
|
27
|
+
end)
|
28
|
+
end
|
29
|
+
|
30
|
+
def registry!
|
31
|
+
Models.mapper self
|
32
|
+
end
|
33
|
+
|
34
|
+
def [] name
|
35
|
+
collection(name.to_sym).repository
|
36
|
+
end
|
37
|
+
|
38
|
+
def collection(name, &blk)
|
39
|
+
if block_given?
|
40
|
+
@collections[name] = Mapping::Collection.new(self, name, @coercer, &blk)
|
41
|
+
else
|
42
|
+
@collections[name] or raise Mapping::UnmappedCollectionError.new(name)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative 'mapping/collection'
|
2
|
+
require_relative 'mapping/coercer'
|
3
|
+
require_relative 'mapping/virtual_proxy'
|
4
|
+
require_relative 'mapping/referencer'
|
5
|
+
require_relative 'mapping/dereferencer'
|
6
|
+
|
7
|
+
module Locomotive
|
8
|
+
module Mapping
|
9
|
+
|
10
|
+
class UnmappedCollectionError < ::StandardError
|
11
|
+
def initialize(name)
|
12
|
+
super("Cannot find collection: #{ name }")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mapping
|
3
|
+
class Coercer
|
4
|
+
|
5
|
+
def initialize(collection)
|
6
|
+
@collection = collection
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_record(entity)
|
10
|
+
{}.tap do |_attributes|
|
11
|
+
_attributes[:id] = entity.id
|
12
|
+
@collection.attributes.each do |name, options|
|
13
|
+
if options[:localized]
|
14
|
+
_attributes[name] = to_locale(entity.send(name))
|
15
|
+
elsif options[:association]
|
16
|
+
Referencer.new(@collection, _attributes, name, options, entity.send(name)).reference!
|
17
|
+
else
|
18
|
+
_attributes[name] = entity.send(name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def from_record(record)
|
25
|
+
@collection.entity.new(id: record[:id]).tap do |_entity|
|
26
|
+
@collection.attributes.each do |name, options|
|
27
|
+
if options[:association]
|
28
|
+
Dereferencer.new(_entity, name, options, record).deference!
|
29
|
+
elsif options[:localized]
|
30
|
+
_entity.send(:"#{name}=", Fields::I18nField.new(record[name]))
|
31
|
+
else
|
32
|
+
_entity.send(:"#{name}=", cast(record[name], options))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def cast value, options
|
41
|
+
if (klass = options.fetch(:klass, nil))
|
42
|
+
klass.new(value)
|
43
|
+
else
|
44
|
+
value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_locale(content)
|
49
|
+
case content
|
50
|
+
when Fields::I18nField
|
51
|
+
content.i18n_values
|
52
|
+
when Hash
|
53
|
+
content
|
54
|
+
when nil
|
55
|
+
{}
|
56
|
+
else
|
57
|
+
raise Fields::I18nField::UnsupportedFormat
|
58
|
+
.new('Localized field needs Fields::I18nField, please use << instead of =')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mapping
|
3
|
+
|
4
|
+
class Collection
|
5
|
+
|
6
|
+
REPOSITORY_SUFFIX = 'Repository'.freeze
|
7
|
+
|
8
|
+
attr_reader :name
|
9
|
+
attr_reader :coercer_class
|
10
|
+
attr_reader :attributes
|
11
|
+
|
12
|
+
def initialize(mapper, name, coercer_class, &blk)
|
13
|
+
@name, @coercer_class, @attributes = name, coercer_class, {}
|
14
|
+
@mapper = mapper
|
15
|
+
|
16
|
+
instance_eval(&blk) if block_given?
|
17
|
+
|
18
|
+
load!
|
19
|
+
end
|
20
|
+
|
21
|
+
# TODO Should be guessed too
|
22
|
+
def repository(klass = nil)
|
23
|
+
if klass
|
24
|
+
@repository = klass.new @mapper
|
25
|
+
else
|
26
|
+
@repository
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def entity(klass = nil)
|
31
|
+
if klass
|
32
|
+
@entity = klass
|
33
|
+
else
|
34
|
+
@entity
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def identity(name = nil)
|
39
|
+
if name
|
40
|
+
@identity = name
|
41
|
+
else
|
42
|
+
@identity || :id
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def attribute(name, options = {})
|
47
|
+
@attributes[name] = options
|
48
|
+
end
|
49
|
+
|
50
|
+
def serialize(entity)
|
51
|
+
@coercer.to_record(entity)
|
52
|
+
end
|
53
|
+
|
54
|
+
def deserialize(records)
|
55
|
+
records.map do |record|
|
56
|
+
@coercer.from_record(record)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def load!
|
63
|
+
@coercer = coercer_class.new(self)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|