Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shotengai
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
shotengai
Commits
f672f229
Commit
f672f229
authored
Oct 13, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add generate methods for value column and template column
parent
22ef5cd0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
9 deletions
+40
-9
json_column.rb
lib/shotengai/json_column.rb
+40
-9
No files found.
lib/shotengai/json_column.rb
View file @
f672f229
...
...
@@ -5,22 +5,23 @@ module Shotengai
end
class_methods
do
def
custom_hash_columns
*
columns
def
custom_hash_columns
columns
,
options
=
{}
decode_or_not
=
options
[
:decode
]
columns
.
each
do
|
column
|
# QUESTION: 这样可以避免 send("#{column}="), 合适?
class_eval
%Q{
define_method('
#{
column
}
') do
super() || {}
end
if
#{
decode_or_not
}
define_method("
#{
column
}
_input=") do |val|
parsed_val = val && val.map{ |h| { (h[:key] || h['key']) => (h[:val] || h['val']) } }.reduce(&:merge)
self.
#{
column
}
= parsed_val
end
define_method("
#{
column
}
_input=") do |val|
parsed_val = val && val.map{ |h| { (h[:key] || h['key']) => (h[:val] || h['val']) } }.reduce(&:merge)
self.
#{
column
}
= parsed_val
self.
#{
column
}
_will_change!
end
define_method("
#{
column
}
_output") do
self.
#{
column
}
.map {|key, val| { key: key, val: val } }
define_method("
#{
column
}
_output") do
self.
#{
column
}
.map {|key, val| { key: key, val: val } }
end
end
}
end
...
...
@@ -30,6 +31,36 @@ module Shotengai
# like meta, detail these json using for code development
end
def
generate_hash_template_column_for
names
names
.
each
do
|
name
|
class_eval
%Q{
def
#{
name
}
_value
Shotengai::Harray.new(super())
end
def
#{
name
}
_value= val
raise Shotengai::WebError.new('
#{
name
}
_val 必须是个 Array'), -1 , 401) unless val.nil? || Array === val
super(val)
end
}
end
end
def
generate_hash_value_column_for
names
,
options
=
{}
names
.
each
do
|
name
|
class_eval
%Q{
delegate :
#{
name
}
_template, to: :
#{
options
[
:delegate_template_to
]
}
if self.respond_to?(
#{
options
[
:delegate_template_to
]
}
)
def
#{
name
}
{
template: self.
#{
name
}
_template,
value: self.
#{
name
}
_value,
}
end
}
end
end
def
column_has_children
column
,
options
ArgumentError
.
new
(
"Please give
#{
column
}
one child at least."
)
unless
options
[
:children
]
children_names
=
options
[
:children
].
map
(
&
:to_s
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment