Add basic field management features
This commit is contained in:
parent
f0a32f1ff4
commit
48f37cc15b
40
lib/docurest/field.rb
Normal file
40
lib/docurest/field.rb
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
require "docurest/field/base"
|
||||||
|
require "docurest/field/boolean"
|
||||||
|
require "docurest/field/date_time"
|
||||||
|
require "docurest/field/integer"
|
||||||
|
require "docurest/field/float"
|
||||||
|
|
||||||
|
module Docurest
|
||||||
|
module Field
|
||||||
|
CONVERSION = {
|
||||||
|
boolean: Docurest::Field::Boolean,
|
||||||
|
integer: Docurest::Field::Integer,
|
||||||
|
float: Docurest::Field::Float,
|
||||||
|
date_time: Docurest::Field::DateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
def field(name, docusign_name = nil, type = nil)
|
||||||
|
attr_accessor name
|
||||||
|
if docusign_name
|
||||||
|
alias_method docusign_name, name
|
||||||
|
define_method("#{docusign_name}=") do |value|
|
||||||
|
value = if type.is_a?(Proc)
|
||||||
|
type.call value
|
||||||
|
elsif CONVERSION.key?(type)
|
||||||
|
CONVERSION[type].new(value).convert
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
|
send("#{name}=", value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def association(klass, &block)
|
||||||
|
define_method(klass) do
|
||||||
|
instance_variable_get(:"@#{klass}") ||
|
||||||
|
instance_variable_set(:"@#{klass}", persisted? ? instance_eval(&block) : [])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
6
lib/docurest/field/base.rb
Normal file
6
lib/docurest/field/base.rb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
module Docurest
|
||||||
|
module Field
|
||||||
|
class Base < Struct.new(:value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
9
lib/docurest/field/boolean.rb
Normal file
9
lib/docurest/field/boolean.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
module Docurest
|
||||||
|
module Field
|
||||||
|
class Boolean < Docurest::Field::Base
|
||||||
|
def convert
|
||||||
|
value == "true" ? true : false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
9
lib/docurest/field/date_time.rb
Normal file
9
lib/docurest/field/date_time.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
module Docurest
|
||||||
|
module Field
|
||||||
|
class DateTime < Docurest::Field::Base
|
||||||
|
def convert
|
||||||
|
::DateTime.parse(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
9
lib/docurest/field/float.rb
Normal file
9
lib/docurest/field/float.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
module Docurest
|
||||||
|
module Field
|
||||||
|
class Float < Docurest::Field::Base
|
||||||
|
def convert
|
||||||
|
value.to_f
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
9
lib/docurest/field/integer.rb
Normal file
9
lib/docurest/field/integer.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
module Docurest
|
||||||
|
module Field
|
||||||
|
class Integer < Docurest::Field::Base
|
||||||
|
def convert
|
||||||
|
value.to_i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user