active_data 0.0.1
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/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +14 -0
- data/Guardfile +19 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/active_data.gemspec +22 -0
- data/lib/active_data/model/attributable.rb +118 -0
- data/lib/active_data/model/collectionizable/proxy.rb +42 -0
- data/lib/active_data/model/collectionizable.rb +55 -0
- data/lib/active_data/model/extensions/array.rb +28 -0
- data/lib/active_data/model/extensions/boolean.rb +30 -0
- data/lib/active_data/model/extensions/date.rb +24 -0
- data/lib/active_data/model/extensions/hash.rb +26 -0
- data/lib/active_data/model/extensions/integer.rb +17 -0
- data/lib/active_data/model/extensions/string.rb +17 -0
- data/lib/active_data/model/extensions.rb +5 -0
- data/lib/active_data/model/serializable.rb +18 -0
- data/lib/active_data/model.rb +70 -0
- data/lib/active_data/version.rb +3 -0
- data/lib/active_data.rb +9 -0
- data/spec/lib/active_data/model/attributable_spec.rb +35 -0
- data/spec/lib/active_data/model/collectionizable_spec.rb +34 -0
- data/spec/lib/active_data/model/serializable_spec.rb +60 -0
- data/spec/lib/active_data/model_spec.rb +23 -0
- data/spec/spec_helper.rb +6 -0
- metadata +143 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create 1.9.3@active_data
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2 do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
# Capybara request specs
|
17
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
|
18
|
+
end
|
19
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 pyromaniac
|
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,29 @@
|
|
1
|
+
# ActiveData
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'active_data'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install active_data
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/active_data.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/active_data/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["pyromaniac"]
|
6
|
+
gem.email = ["kinwizard@gmail.com"]
|
7
|
+
gem.description = %q{Making object from any hash or hash array}
|
8
|
+
gem.summary = %q{Working with hashes in AR style}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "active_data"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActiveData::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rake"
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
gem.add_runtime_dependency "activesupport"
|
21
|
+
gem.add_runtime_dependency "activemodel"
|
22
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'active_data/model/serializable'
|
2
|
+
|
3
|
+
module ActiveData
|
4
|
+
module Model
|
5
|
+
module Attributable
|
6
|
+
include Serializable
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
class_attribute :_attributes, :instance_reader => false, :instance_writer => false
|
11
|
+
self._attributes = ActiveSupport::HashWithIndifferentAccess.new
|
12
|
+
|
13
|
+
delegate :attribute_default, :to => 'self.class'
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def attribute name, options = {}, &block
|
18
|
+
default = options.is_a?(Hash) ? options[:default] : options
|
19
|
+
type = options.is_a?(Hash) ? normalize_type(options[:type]) : String
|
20
|
+
self._attributes = self._attributes.merge(name => {
|
21
|
+
default: (block || default),
|
22
|
+
type: type
|
23
|
+
})
|
24
|
+
|
25
|
+
define_method name do
|
26
|
+
read_attribute(name)
|
27
|
+
end
|
28
|
+
define_method "#{name}_before_type_cast" do
|
29
|
+
read_attribute_before_type_cast(name)
|
30
|
+
end
|
31
|
+
define_method "#{name}?" do
|
32
|
+
read_attribute(name).present?
|
33
|
+
end
|
34
|
+
define_method "#{name}=" do |value|
|
35
|
+
write_attribute(name, value)
|
36
|
+
end
|
37
|
+
|
38
|
+
if options.is_a?(Hash) && options[:in]
|
39
|
+
define_singleton_method "#{name}_values" do
|
40
|
+
options[:in].dup
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def normalize_type type
|
46
|
+
case type
|
47
|
+
when String, Symbol then
|
48
|
+
type.to_s.camelize.safe_constantize
|
49
|
+
when nil then
|
50
|
+
String
|
51
|
+
else
|
52
|
+
type
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def attribute_default name
|
57
|
+
default = _attributes[name][:default]
|
58
|
+
default.respond_to?(:call) ? default.call : default
|
59
|
+
end
|
60
|
+
|
61
|
+
def initialize_attributes
|
62
|
+
_attributes.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, (name, value)|
|
63
|
+
result[name] = nil
|
64
|
+
result
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def read_attribute name
|
70
|
+
@attributes[name].nil? ? attribute_default(name) : @attributes[name]
|
71
|
+
end
|
72
|
+
alias_method :[], :read_attribute
|
73
|
+
|
74
|
+
def read_attribute_before_type_cast name
|
75
|
+
deserialize(send(name))
|
76
|
+
end
|
77
|
+
|
78
|
+
def write_attribute name, value
|
79
|
+
type = self.class._attributes[name][:type]
|
80
|
+
@attributes[name] = serialize(value, type)
|
81
|
+
end
|
82
|
+
alias_method :[]=, :write_attribute
|
83
|
+
|
84
|
+
def attributes
|
85
|
+
Hash[attribute_names.map { |name| [name, send(name)] }]
|
86
|
+
end
|
87
|
+
|
88
|
+
def present_attributes
|
89
|
+
Hash[attribute_names.map do |name|
|
90
|
+
value = send(name)
|
91
|
+
[name, value] if value.present?
|
92
|
+
end]
|
93
|
+
end
|
94
|
+
|
95
|
+
def attribute_names
|
96
|
+
@attributes.keys
|
97
|
+
end
|
98
|
+
|
99
|
+
def attributes= attributes
|
100
|
+
assign_attributes(attributes)
|
101
|
+
end
|
102
|
+
|
103
|
+
def update_attributes attributes
|
104
|
+
self.attributes = attributes
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def assign_attributes attributes
|
110
|
+
(attributes.presence || {}).each do |(name, value)|
|
111
|
+
send("#{name}=", value) if respond_to?("#{name}=")
|
112
|
+
end
|
113
|
+
self.attributes
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ActiveData
|
2
|
+
module Model
|
3
|
+
module Collectionizable
|
4
|
+
module Proxy
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :collectible
|
9
|
+
|
10
|
+
def initialize source = nil
|
11
|
+
source ||= self.class.superclass.new
|
12
|
+
super source.map { |entity| collectible.instantiate(entity) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def respond_to? method
|
17
|
+
super || collectible.respond_to?(method)
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing method, *args, &block
|
21
|
+
result = with_scope { collectible.send(method, *args, &block) }
|
22
|
+
result = self.class.new result if result.instance_of? self.class.superclass
|
23
|
+
result
|
24
|
+
end
|
25
|
+
|
26
|
+
def with_scope
|
27
|
+
previous_scope = collectible.current_scope
|
28
|
+
collectible.current_scope = self
|
29
|
+
result = yield
|
30
|
+
collectible.current_scope = previous_scope
|
31
|
+
result
|
32
|
+
end
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
def modelize value
|
36
|
+
new value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'active_data/model/collectionizable/proxy'
|
2
|
+
|
3
|
+
module ActiveData
|
4
|
+
module Model
|
5
|
+
module Collectionizable
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
collectionize
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def collectionize collection_superclass = nil
|
15
|
+
collection_superclass ||= Array
|
16
|
+
collection = Class.new(collection_superclass) do
|
17
|
+
include ActiveData::Model::Collectionizable::Proxy
|
18
|
+
end
|
19
|
+
collection.collectible = self
|
20
|
+
|
21
|
+
remove_const :Collection if const_defined? :Collection
|
22
|
+
const_set :Collection, collection
|
23
|
+
end
|
24
|
+
|
25
|
+
def respond_to? method
|
26
|
+
super || collection_class.superclass.method_defined?(method)
|
27
|
+
end
|
28
|
+
|
29
|
+
def method_missing method, *args, &block
|
30
|
+
current_scope.send(method, *args, &block) if collection_class.superclass.method_defined?(method)
|
31
|
+
end
|
32
|
+
|
33
|
+
def collection source = nil
|
34
|
+
collection_class.new source
|
35
|
+
end
|
36
|
+
|
37
|
+
def collection_class
|
38
|
+
@collection_class ||= const_get(:Collection)
|
39
|
+
end
|
40
|
+
|
41
|
+
def current_scope= value
|
42
|
+
@current_scope = value
|
43
|
+
end
|
44
|
+
|
45
|
+
def current_scope
|
46
|
+
@current_scope ||= collection(load)
|
47
|
+
end
|
48
|
+
alias :scope :current_scope
|
49
|
+
|
50
|
+
def load; end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module ActiveData
|
2
|
+
module Model
|
3
|
+
module Extensions
|
4
|
+
module Array
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def demodelize
|
8
|
+
join(', ')
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def modelize value
|
13
|
+
case value
|
14
|
+
when String then
|
15
|
+
value.split(',').map(&:strip)
|
16
|
+
when Array then
|
17
|
+
value
|
18
|
+
else
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Array.send :include, ActiveData::Model::Extensions::Array
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActiveData
|
2
|
+
module Model
|
3
|
+
module Extensions
|
4
|
+
module Boolean
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
MAPPING = {
|
8
|
+
"1" => true,
|
9
|
+
"0" => false,
|
10
|
+
"t" => true,
|
11
|
+
"f" => false,
|
12
|
+
"T" => true,
|
13
|
+
"F" => false,
|
14
|
+
"true" => true,
|
15
|
+
"false" => false,
|
16
|
+
"TRUE" => true,
|
17
|
+
"FALSE" => false
|
18
|
+
}
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def modelize value
|
22
|
+
MAPPING[value.to_s]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Boolean.send :include, ActiveData::Model::Extensions::Boolean
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActiveData
|
2
|
+
module Model
|
3
|
+
module Extensions
|
4
|
+
module Date
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def modelize value
|
9
|
+
case value
|
10
|
+
when String then
|
11
|
+
Date.parse(value.to_s) rescue nil
|
12
|
+
when Date, DateTime, Time then
|
13
|
+
value.to_date
|
14
|
+
else
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
Date.send :include, ActiveData::Model::Extensions::Date
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ActiveData
|
2
|
+
module Model
|
3
|
+
module Extensions
|
4
|
+
module Hash
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def demodelize
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def modelize value
|
13
|
+
case value
|
14
|
+
when Hash then
|
15
|
+
value
|
16
|
+
else
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Hash.send :include, ActiveData::Model::Extensions::Hash
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveData
|
2
|
+
module Model
|
3
|
+
module Extensions
|
4
|
+
module Integer
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def modelize value
|
9
|
+
value.try(:to_i) if value.to_s =~ /\A\d+\Z/
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Integer.send :include, ActiveData::Model::Extensions::Integer
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveData
|
2
|
+
module Model
|
3
|
+
module Extensions
|
4
|
+
module String
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def modelize value
|
9
|
+
value.to_s if value.present?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
String.send :include, ActiveData::Model::Extensions::String
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveData
|
2
|
+
module Model
|
3
|
+
module Serializable
|
4
|
+
|
5
|
+
class UnknownAttribute < ::StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
def serialize value, type
|
9
|
+
type.modelize(value)
|
10
|
+
end
|
11
|
+
|
12
|
+
def deserialize value
|
13
|
+
value.respond_to? :demodelize ? value.demodelize : value.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'active_data/model/collectionizable'
|
2
|
+
require 'active_data/model/attributable'
|
3
|
+
require 'active_data/model/extensions'
|
4
|
+
|
5
|
+
module ActiveData
|
6
|
+
module Model
|
7
|
+
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
include ActiveModel::Conversion
|
12
|
+
include ActiveModel::Dirty
|
13
|
+
include ActiveModel::Validations
|
14
|
+
include ActiveModel::MassAssignmentSecurity
|
15
|
+
include ActiveModel::Serialization
|
16
|
+
include ActiveModel::Serializers::JSON
|
17
|
+
include ActiveModel::Serializers::Xml
|
18
|
+
|
19
|
+
include Attributable
|
20
|
+
include Collectionizable
|
21
|
+
extend ActiveModel::Callbacks
|
22
|
+
extend ActiveModel::Naming
|
23
|
+
extend ActiveModel::Translation
|
24
|
+
|
25
|
+
self.include_root_in_json = false
|
26
|
+
|
27
|
+
def initialize attributes = {}
|
28
|
+
@attributes = self.class.initialize_attributes
|
29
|
+
@new_record = true
|
30
|
+
assign_attributes attributes
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.i18n_scope
|
34
|
+
:active_data
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
def instantiate attributes = nil
|
40
|
+
attributes ||= {}
|
41
|
+
return attributes if attributes.instance_of? self
|
42
|
+
|
43
|
+
instance = allocate
|
44
|
+
|
45
|
+
instance.instance_variable_set(:@attributes, initialize_attributes)
|
46
|
+
instance.instance_variable_set(:@new_record, false)
|
47
|
+
instance.attributes = attributes
|
48
|
+
|
49
|
+
instance
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def errors
|
54
|
+
@errors ||= ActiveModel::Errors.new(self)
|
55
|
+
end
|
56
|
+
|
57
|
+
def persisted?
|
58
|
+
!@new_record
|
59
|
+
end
|
60
|
+
|
61
|
+
def == other
|
62
|
+
other.instance_of?(self.class) && other.attributes == attributes
|
63
|
+
end
|
64
|
+
|
65
|
+
def assign_attributes(attributes, options = {})
|
66
|
+
super(sanitize_for_mass_assignment((attributes.presence || {}), options[:as]))
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
data/lib/active_data.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiveData::Model::Attributable do
|
5
|
+
|
6
|
+
let(:klass) do
|
7
|
+
Class.new do
|
8
|
+
include ActiveData::Model::Attributable
|
9
|
+
attr_reader :name
|
10
|
+
|
11
|
+
attribute :hello
|
12
|
+
attribute :count, type: :integer, default: 10
|
13
|
+
attribute(:calc, type: :integer) {2 + 3}
|
14
|
+
|
15
|
+
def initialize name = nil
|
16
|
+
@attributes = self.class.initialize_attributes
|
17
|
+
@name = name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context do
|
23
|
+
subject{klass.new('world')}
|
24
|
+
its(:attributes){should == {"hello"=>nil, "count"=>10, "calc"=>5}}
|
25
|
+
its(:present_attributes){should == {"count"=>10, "calc"=>5}}
|
26
|
+
its(:name){should == 'world'}
|
27
|
+
its(:hello){should be_nil}
|
28
|
+
its(:count){should == 10}
|
29
|
+
its(:calc){should == 5}
|
30
|
+
specify{expect{subject.hello = 'worlds'}.to change{subject.hello}.from(nil).to('worlds')}
|
31
|
+
specify{expect{subject.count = 20}.to change{subject.count}.from(10).to(20)}
|
32
|
+
specify{expect{subject.calc = 15}.to change{subject.calc}.from(5).to(15)}
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiveData::Model::Collectionizable do
|
5
|
+
let(:klass) do
|
6
|
+
Class.new do
|
7
|
+
include ActiveData::Model
|
8
|
+
|
9
|
+
attribute :name
|
10
|
+
|
11
|
+
def self.except_first
|
12
|
+
self[1..-1]
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.no_mars
|
16
|
+
delete_if{|i| i.name == 'Mars'}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
let(:collection){klass.collection([{:name => 'Hello'}, {:name => 'World'}, {:name => 'Mars'}])}
|
21
|
+
|
22
|
+
specify{klass.const_defined?('Collection').should be_true}
|
23
|
+
specify{klass.collection_class.should == klass.const_get(:Collection)}
|
24
|
+
specify{klass.collection_class.collectible.should == klass}
|
25
|
+
specify{klass.collection_class.new.should be_empty}
|
26
|
+
|
27
|
+
specify{collection.should be_instance_of klass.collection_class}
|
28
|
+
specify{collection.except_first.should be_instance_of klass.collection_class}
|
29
|
+
specify{collection.no_mars.should be_instance_of klass.collection_class}
|
30
|
+
specify{collection.except_first.should == klass.collection([{:name => 'World'}, {:name => 'Mars'}])}
|
31
|
+
specify{collection.no_mars.should == klass.collection([{:name => 'Hello'}, {:name => 'World'}])}
|
32
|
+
specify{collection.except_first.no_mars.should == klass.collection([{:name => 'World'}])}
|
33
|
+
specify{collection.no_mars.except_first.should == klass.collection([{:name => 'World'}])}
|
34
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiveData::Model::Serializable do
|
5
|
+
|
6
|
+
let(:klass) do
|
7
|
+
Class.new do
|
8
|
+
include ActiveData::Model::Attributable
|
9
|
+
attr_reader :name
|
10
|
+
|
11
|
+
attribute :string, type: String
|
12
|
+
attribute :integer, type: Integer
|
13
|
+
attribute :boolean, type: Boolean
|
14
|
+
attribute :array, type: Array
|
15
|
+
|
16
|
+
def initialize name = nil
|
17
|
+
@attributes = self.class.initialize_attributes
|
18
|
+
@name = name
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
subject{klass.new}
|
24
|
+
|
25
|
+
context 'string' do
|
26
|
+
specify{subject.tap{|s| s.string = 'hello'}.string.should == 'hello'}
|
27
|
+
specify{subject.tap{|s| s.string = 123}.string.should == '123'}
|
28
|
+
specify{subject.tap{|s| s.string = nil}.string.should == nil}
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'integer' do
|
32
|
+
specify{subject.tap{|s| s.integer = 'hello'}.integer.should == nil}
|
33
|
+
specify{subject.tap{|s| s.integer = '123hello'}.integer.should == nil}
|
34
|
+
specify{subject.tap{|s| s.integer = '123'}.integer.should == 123}
|
35
|
+
specify{subject.tap{|s| s.integer = 123}.integer.should == 123}
|
36
|
+
specify{subject.tap{|s| s.integer = nil}.integer.should == nil}
|
37
|
+
specify{subject.tap{|s| s.integer = [123]}.integer.should == nil}
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'boolean' do
|
41
|
+
specify{subject.tap{|s| s.boolean = 'hello'}.boolean.should == nil}
|
42
|
+
specify{subject.tap{|s| s.boolean = 'true'}.boolean.should == true}
|
43
|
+
specify{subject.tap{|s| s.boolean = 'false'}.boolean.should == false}
|
44
|
+
specify{subject.tap{|s| s.boolean = '1'}.boolean.should == true}
|
45
|
+
specify{subject.tap{|s| s.boolean = '0'}.boolean.should == false}
|
46
|
+
specify{subject.tap{|s| s.boolean = true}.boolean.should == true}
|
47
|
+
specify{subject.tap{|s| s.boolean = false}.boolean.should == false}
|
48
|
+
specify{subject.tap{|s| s.boolean = 1}.boolean.should == true}
|
49
|
+
specify{subject.tap{|s| s.boolean = 0}.boolean.should == false}
|
50
|
+
specify{subject.tap{|s| s.boolean = nil}.boolean.should == nil}
|
51
|
+
specify{subject.tap{|s| s.boolean = [123]}.boolean.should == nil}
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'array' do
|
55
|
+
specify{subject.tap{|s| s.array = [1, 2, 3]}.array.should == [1, 2, 3]}
|
56
|
+
specify{subject.tap{|s| s.array = 'hello, world'}.array.should == ['hello', 'world']}
|
57
|
+
specify{subject.tap{|s| s.array = 10}.array.should == nil}
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ActiveData::Model do
|
5
|
+
|
6
|
+
let(:model) do
|
7
|
+
Class.new do
|
8
|
+
include ActiveData::Model
|
9
|
+
|
10
|
+
attribute :name
|
11
|
+
attribute :count, default: 0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
specify{model.i18n_scope.should == :active_data}
|
16
|
+
specify{model.new.should_not be_persisted}
|
17
|
+
specify{model.instantiate.should be_an_instance_of model}
|
18
|
+
specify{model.instantiate.should be_persisted}
|
19
|
+
|
20
|
+
context 'Fault tolerance' do
|
21
|
+
specify{expect{model.new(:foo => 'bar')}.not_to raise_error}
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- pyromaniac
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
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: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
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: activesupport
|
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: activemodel
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Making object from any hash or hash array
|
79
|
+
email:
|
80
|
+
- kinwizard@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- .rvmrc
|
88
|
+
- .travis.yml
|
89
|
+
- Gemfile
|
90
|
+
- Guardfile
|
91
|
+
- LICENSE
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- active_data.gemspec
|
95
|
+
- lib/active_data.rb
|
96
|
+
- lib/active_data/model.rb
|
97
|
+
- lib/active_data/model/attributable.rb
|
98
|
+
- lib/active_data/model/collectionizable.rb
|
99
|
+
- lib/active_data/model/collectionizable/proxy.rb
|
100
|
+
- lib/active_data/model/extensions.rb
|
101
|
+
- lib/active_data/model/extensions/array.rb
|
102
|
+
- lib/active_data/model/extensions/boolean.rb
|
103
|
+
- lib/active_data/model/extensions/date.rb
|
104
|
+
- lib/active_data/model/extensions/hash.rb
|
105
|
+
- lib/active_data/model/extensions/integer.rb
|
106
|
+
- lib/active_data/model/extensions/string.rb
|
107
|
+
- lib/active_data/model/serializable.rb
|
108
|
+
- lib/active_data/version.rb
|
109
|
+
- spec/lib/active_data/model/attributable_spec.rb
|
110
|
+
- spec/lib/active_data/model/collectionizable_spec.rb
|
111
|
+
- spec/lib/active_data/model/serializable_spec.rb
|
112
|
+
- spec/lib/active_data/model_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
homepage: ''
|
115
|
+
licenses: []
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 1.8.24
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: Working with hashes in AR style
|
138
|
+
test_files:
|
139
|
+
- spec/lib/active_data/model/attributable_spec.rb
|
140
|
+
- spec/lib/active_data/model/collectionizable_spec.rb
|
141
|
+
- spec/lib/active_data/model/serializable_spec.rb
|
142
|
+
- spec/lib/active_data/model_spec.rb
|
143
|
+
- spec/spec_helper.rb
|