simplemodel 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/LICENSE +20 -0
- data/README +24 -0
- data/Rakefile +14 -0
- data/VERSION +1 -0
- data/lib/simple_model.rb +1 -0
- data/lib/simplemodel.rb +13 -0
- data/lib/simplemodel/association.rb +87 -0
- data/lib/simplemodel/base.rb +117 -0
- data/lib/simplemodel/ext/typed_array.rb +27 -0
- data/simplemodel.gemspec +48 -0
- metadata +73 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Antoine Legrand
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Simple 'In-memory' models description with associations.
|
2
|
+
|
3
|
+
Primarily developed to create or parse JSON to easily communicate with any REST API.
|
4
|
+
exemple: http://github.com/antlegrand/sitescout
|
5
|
+
|
6
|
+
|
7
|
+
Examples:
|
8
|
+
|
9
|
+
require "simplemodel"
|
10
|
+
class Link < SimpleModel::Base
|
11
|
+
include SimpleModel::Association
|
12
|
+
define_attributes :rel, :href
|
13
|
+
end
|
14
|
+
|
15
|
+
class Offer < SiteT::Models::Base
|
16
|
+
include SimpleModel::Association
|
17
|
+
define_attributes :name, :offerId, :defaultRevenue
|
18
|
+
one :onelink, :class_name => Link
|
19
|
+
many :links, :class_name => Link
|
20
|
+
many :toto, :class_name => Link
|
21
|
+
one :titi, :class_name => Link
|
22
|
+
|
23
|
+
class Test < SuperModel::Base
|
24
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "simplemodel"
|
5
|
+
gemspec.summary = "In memory ActiveModel with associations"
|
6
|
+
gemspec.email = "ant.legrand@gmail.com"
|
7
|
+
gemspec.homepage = "http://github.com/alegrand/simplemodel"
|
8
|
+
gemspec.description = "In memory activemodel with associations"
|
9
|
+
gemspec.authors = ["Antoine Legrand"]
|
10
|
+
gemspec.add_dependency("activemodel", "~> 3.0.0")
|
11
|
+
end
|
12
|
+
rescue LoadError
|
13
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
14
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.9.1
|
data/lib/simple_model.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "simplemodel")
|
data/lib/simplemodel.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "active_support/core_ext/class/attribute_accessors"
|
2
|
+
require "active_support/core_ext/class/attribute"
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require "active_model"
|
5
|
+
|
6
|
+
$:.unshift(File.dirname(__FILE__))
|
7
|
+
require "simplemodel/ext/typed_array"
|
8
|
+
module SimpleModel
|
9
|
+
autoload :Association, "simplemodel/association"
|
10
|
+
autoload :Base, "simplemodel/base"
|
11
|
+
end
|
12
|
+
|
13
|
+
SimpleModel::Base.include_root_in_json = false
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "active_support/core_ext/string/inflections.rb"
|
2
|
+
|
3
|
+
module SimpleModel
|
4
|
+
module Association
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
def associations
|
8
|
+
@associations ||= {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def one(to_model, options = {})
|
12
|
+
to_model = to_model.to_s
|
13
|
+
class_name = options[:class_name] || to_model.classify
|
14
|
+
self.associations[to_model.to_s.to_sym] = {:class_name => class_name, :name => to_model}
|
15
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
16
|
+
attr_reader :#{to_model}
|
17
|
+
|
18
|
+
def #{to_model}
|
19
|
+
@#{to_model}
|
20
|
+
end#
|
21
|
+
|
22
|
+
def clear_#{to_model}
|
23
|
+
@#{to_model} = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def #{to_model}=(elt)
|
27
|
+
if elt.kind_of?(Hash)
|
28
|
+
instance = #{class_name}.new(elt)
|
29
|
+
elsif elt.instance_of?(#{class_name}) || elt == nil
|
30
|
+
instance = elt
|
31
|
+
else
|
32
|
+
raise TypeError, "Expected Hash of attributes or #{class_name.name}"
|
33
|
+
end
|
34
|
+
@#{to_model} = instance
|
35
|
+
end
|
36
|
+
|
37
|
+
EOS
|
38
|
+
end
|
39
|
+
|
40
|
+
def many(to_model, options = {})
|
41
|
+
to_model = to_model.to_s
|
42
|
+
class_name = options[:class_name] || to_model.classify
|
43
|
+
name = to_model
|
44
|
+
self.associations[to_model.to_s.to_sym] = {:class_name => class_name, :name => to_model}
|
45
|
+
|
46
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
47
|
+
attr_reader :#{to_model}
|
48
|
+
|
49
|
+
def #{to_model}
|
50
|
+
@#{to_model} ||= TypedArray.new(#{class_name})
|
51
|
+
end#
|
52
|
+
|
53
|
+
def clear_#{to_model}
|
54
|
+
@#{to_model} = nil
|
55
|
+
end
|
56
|
+
|
57
|
+
def #{to_model}=(array)
|
58
|
+
@#{to_model} = TypedArray.new(#{class_name})
|
59
|
+
array.each do |elt|
|
60
|
+
if elt.kind_of?(Hash)
|
61
|
+
instance = #{class_name}.new(elt)
|
62
|
+
else
|
63
|
+
instance = elt
|
64
|
+
end
|
65
|
+
self.#{to_model} << instance
|
66
|
+
end
|
67
|
+
return self.#{to_model}
|
68
|
+
end
|
69
|
+
|
70
|
+
def add_#{to_model}(*args)
|
71
|
+
@#{to_model} = TypedArray.new(#{class_name})
|
72
|
+
args.each do |attr|
|
73
|
+
instance = #{class_name}.new(attr)
|
74
|
+
self.#{to_model} << instance
|
75
|
+
end
|
76
|
+
end
|
77
|
+
EOS
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
module Model
|
82
|
+
def self.included(base)
|
83
|
+
base.extend(ClassMethods)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module SimpleModel
|
2
|
+
class Base
|
3
|
+
class_attribute :known_attributes, :raw_attributes
|
4
|
+
self.known_attributes = []
|
5
|
+
self.raw_attributes = {}
|
6
|
+
class << self
|
7
|
+
def define_attributes(*attr)
|
8
|
+
self.raw_attributes = {}
|
9
|
+
attr.each{|a| self.raw_attributes[a] = nil}
|
10
|
+
attributes *attr
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes(*attributes)
|
14
|
+
self.known_attributes |= attributes.map(&:to_s)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_accessor :attributes, :unknown_attributes
|
19
|
+
|
20
|
+
def refresh()
|
21
|
+
self.attributes.each do |name, attr|
|
22
|
+
res = self.send "#{name}=".to_sym, attr
|
23
|
+
if not self.raw_attributes.has_key?(name.to_s.to_sym)
|
24
|
+
self.attributes.delete(name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
return self
|
28
|
+
end
|
29
|
+
|
30
|
+
def known_attributes
|
31
|
+
self.class.known_attributes | self.attributes.keys.map(&:to_s)
|
32
|
+
end
|
33
|
+
|
34
|
+
def raw_attributes
|
35
|
+
self.class.raw_attributes
|
36
|
+
end
|
37
|
+
|
38
|
+
def attributes
|
39
|
+
@attributes
|
40
|
+
end
|
41
|
+
|
42
|
+
def attributes=(attr)
|
43
|
+
@attributes = {}.with_indifferent_access
|
44
|
+
@attributes.merge!(self.raw_attributes)
|
45
|
+
@attributes.merge!(attr)
|
46
|
+
self.refresh()
|
47
|
+
return @attributes
|
48
|
+
end
|
49
|
+
|
50
|
+
def initialize(opts = {})
|
51
|
+
@unknown_attributes = {}
|
52
|
+
self.attributes = opts
|
53
|
+
end
|
54
|
+
|
55
|
+
def has_attribute?(name)
|
56
|
+
self.attributes.has_key?(name)
|
57
|
+
end
|
58
|
+
|
59
|
+
alias_method :respond_to_without_attributes?, :respond_to?
|
60
|
+
|
61
|
+
def respond_to?(method, include_priv = false)
|
62
|
+
method_name = method.to_s
|
63
|
+
if self.attributes.nil?
|
64
|
+
super
|
65
|
+
elsif known_attributes.include?(method_name)
|
66
|
+
true
|
67
|
+
elsif method_name =~ /(?:=|\?)$/ && self.attributes.include?($`)
|
68
|
+
true
|
69
|
+
else
|
70
|
+
super
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
protected
|
76
|
+
def read_attribute(name)
|
77
|
+
@attributes[name]
|
78
|
+
end
|
79
|
+
|
80
|
+
def write_attribute(name, value)
|
81
|
+
@attributes[name] = value
|
82
|
+
self.refresh()
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def method_missing(method_symbol, *arguments) #:nodoc:
|
88
|
+
method_name = method_symbol.to_s
|
89
|
+
if method_name =~ /(=|\?)$/
|
90
|
+
case $1
|
91
|
+
when "="
|
92
|
+
if not self.raw_attributes.has_key?($`.to_s.to_sym)
|
93
|
+
self.attributes.delete($`)
|
94
|
+
self.unknown_attributes[$`] = arguments.first
|
95
|
+
else
|
96
|
+
@attributes[$`] = arguments.first
|
97
|
+
end
|
98
|
+
when "?"
|
99
|
+
@attributes.has_key?($`)
|
100
|
+
end
|
101
|
+
else
|
102
|
+
return attributes[method_name] if attributes.has_key?(method_name)
|
103
|
+
super
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
class Base
|
110
|
+
extend ActiveModel::Naming
|
111
|
+
include ActiveModel::Conversion
|
112
|
+
include ActiveModel::Serializers::JSON
|
113
|
+
include ActiveModel::Serializers::Xml
|
114
|
+
include Association::Model
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class TypedArray < Array
|
2
|
+
|
3
|
+
def initialize(klass, *args)
|
4
|
+
@klass = klass
|
5
|
+
super *args
|
6
|
+
end
|
7
|
+
|
8
|
+
def <<(elt)
|
9
|
+
check_klass(elt)
|
10
|
+
super(elt)
|
11
|
+
end
|
12
|
+
|
13
|
+
def push(*args)
|
14
|
+
check_klass(*args)
|
15
|
+
super *args
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def check_klass(*elts)
|
20
|
+
elts.each do |e|
|
21
|
+
if not e.instance_of?(@klass)
|
22
|
+
raise TypeError, "can't convert #{e.class.name} into #{@klass.name}: #{e}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
return true
|
26
|
+
end
|
27
|
+
end
|
data/simplemodel.gemspec
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# FORKED FROM 'SuperModel' BY Alex MacCaw
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{simplemodel}
|
8
|
+
s.version = "0.9.1"
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.authors = ["Antoine Legrand"]
|
11
|
+
s.date = %q{2013-08-01}
|
12
|
+
s.description = %q{In memory using ActiveModel with associations}
|
13
|
+
s.email = %q{ant.legrand@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"LICENSE",
|
20
|
+
"README",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"lib/simple_model.rb",
|
24
|
+
"lib/simplemodel.rb",
|
25
|
+
"lib/simplemodel/association.rb",
|
26
|
+
"lib/simplemodel/base.rb",
|
27
|
+
"lib/simplemodel/ext/typed_array.rb",
|
28
|
+
"simplemodel.gemspec"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/alegrand/simplemodel}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.3.7}
|
34
|
+
s.summary = %q{In memory using ActiveModel}
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
38
|
+
s.specification_version = 3
|
39
|
+
|
40
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
41
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
|
42
|
+
else
|
43
|
+
s.add_dependency(%q<activemodel>, [">= 3.0.0"])
|
44
|
+
end
|
45
|
+
else
|
46
|
+
s.add_dependency(%q<activemodel>, [">= 3.0.0"])
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simplemodel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Antoine Legrand
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.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: 3.0.0
|
30
|
+
description: In memory using ActiveModel with associations
|
31
|
+
email: ant.legrand@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files:
|
35
|
+
- README
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- LICENSE
|
39
|
+
- README
|
40
|
+
- Rakefile
|
41
|
+
- VERSION
|
42
|
+
- lib/simple_model.rb
|
43
|
+
- lib/simplemodel.rb
|
44
|
+
- lib/simplemodel/association.rb
|
45
|
+
- lib/simplemodel/base.rb
|
46
|
+
- lib/simplemodel/ext/typed_array.rb
|
47
|
+
- simplemodel.gemspec
|
48
|
+
homepage: http://github.com/alegrand/simplemodel
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.24
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: In memory using ActiveModel
|
73
|
+
test_files: []
|