mamechiwa 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mamechiwa/integrations/active_record.rb +7 -0
- data/lib/mamechiwa/version.rb +5 -0
- data/lib/mamechiwa.rb +132 -0
- metadata +127 -0
data/lib/mamechiwa.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
module Mamechiwa
|
4
|
+
module ClassMethods
|
5
|
+
attr_accessor :mame_config, :mame_scope, :mame_group_field
|
6
|
+
|
7
|
+
def mame_config
|
8
|
+
@mame_config ||= Hash.new{ |h,k| h[k] = {} }
|
9
|
+
end
|
10
|
+
|
11
|
+
def mame_scope
|
12
|
+
@mame_scope ||= ""
|
13
|
+
end
|
14
|
+
|
15
|
+
def mamechiwa(field, group_field="")
|
16
|
+
self.mame_group_field = group_field
|
17
|
+
|
18
|
+
yield self
|
19
|
+
|
20
|
+
self.mame_config.each do |k, scope|
|
21
|
+
clazz = create_embedded_class(scope[:attrs])
|
22
|
+
clazz.class_eval &scope[:validator] if scope[:validator]
|
23
|
+
|
24
|
+
scope[:class] = clazz
|
25
|
+
end
|
26
|
+
|
27
|
+
class_eval do
|
28
|
+
define_method :initialize_with_mame do
|
29
|
+
unless @mame_embedded
|
30
|
+
group = self.class.mame_group_field.size > 0 ? self.send(self.class.mame_group_field).to_s : ""
|
31
|
+
@mame_embedded = self.class.mame_config[group][:class].new(self, field)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method "#{field}" do
|
36
|
+
initialize_with_mame
|
37
|
+
|
38
|
+
@mame_embedded
|
39
|
+
end
|
40
|
+
|
41
|
+
validate :mame_validator
|
42
|
+
|
43
|
+
define_method :mame_validator do
|
44
|
+
initialize_with_mame
|
45
|
+
|
46
|
+
if !@mame_embedded.valid?
|
47
|
+
@mame_embedded.errors.each do |k, v|
|
48
|
+
errors.add("#{field}[#{k}]", v)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def mattr(*name)
|
56
|
+
mame_config[mame_scope][:attrs] ||= []
|
57
|
+
mame_config[mame_scope][:attrs] += [name].flatten
|
58
|
+
mame_config[mame_scope][:attrs].uniq!
|
59
|
+
end
|
60
|
+
|
61
|
+
def define_validator(&validator)
|
62
|
+
mame_config[mame_scope][:validator] = validator
|
63
|
+
end
|
64
|
+
|
65
|
+
def mame_group(name)
|
66
|
+
self.mame_scope = name
|
67
|
+
yield self
|
68
|
+
self.mame_scope = ""
|
69
|
+
end
|
70
|
+
|
71
|
+
def create_embedded_class(attrs)
|
72
|
+
clazz = Class.new(Hash) do
|
73
|
+
include ActiveModel::Validations
|
74
|
+
|
75
|
+
define_method :mame_attrs do
|
76
|
+
attrs
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.name
|
80
|
+
"mamechiwa_embedded"
|
81
|
+
end
|
82
|
+
|
83
|
+
def initialize(parent, field)
|
84
|
+
@mame_parent = parent
|
85
|
+
@mame_parent_field = field
|
86
|
+
|
87
|
+
value = @mame_parent.send(:[], @mame_parent_field)
|
88
|
+
|
89
|
+
mame_attrs.each do |attr|
|
90
|
+
self[attr.to_s] = nil
|
91
|
+
end
|
92
|
+
|
93
|
+
if value
|
94
|
+
self.merge!(ActiveSupport::JSON.decode(value))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def serialize
|
99
|
+
self.to_json
|
100
|
+
end
|
101
|
+
|
102
|
+
alias :old_get :[]
|
103
|
+
|
104
|
+
def [](arg)
|
105
|
+
self.old_get(arg)
|
106
|
+
end
|
107
|
+
|
108
|
+
alias :old_set :[]=
|
109
|
+
|
110
|
+
def []=(*args)
|
111
|
+
key, value = args
|
112
|
+
self.old_set(key, value)
|
113
|
+
@mame_parent.send(:write_attribute, @mame_parent_field.to_sym, self.serialize)
|
114
|
+
end
|
115
|
+
|
116
|
+
attrs.each do |attr|
|
117
|
+
class_eval <<-EOF
|
118
|
+
def #{attr}
|
119
|
+
self["#{attr}"]
|
120
|
+
end
|
121
|
+
|
122
|
+
def #{attr}=(value)
|
123
|
+
self["#{attr}"] = value
|
124
|
+
end
|
125
|
+
EOF
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
require "mamechiwa/integrations/active_record"
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mamechiwa
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- sutetotanuki
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
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: mysql2
|
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: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
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: mocha
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
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: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: database_cleaner
|
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
|
+
description: Seriarizer to store databae via active record. murina mono ha muri!
|
95
|
+
email: sutetotanuki@gmail.com
|
96
|
+
executables: []
|
97
|
+
extensions: []
|
98
|
+
extra_rdoc_files: []
|
99
|
+
files:
|
100
|
+
- lib/mamechiwa/integrations/active_record.rb
|
101
|
+
- lib/mamechiwa/version.rb
|
102
|
+
- lib/mamechiwa.rb
|
103
|
+
homepage: https://github.com/sutetotanuki/mamechiwa
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.23
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: hanako kawaii
|
127
|
+
test_files: []
|