-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Field
class
#11
Add Field
class
#11
Changes from all commits
7964360
96218ec
13aff01
c7eb84e
42fa037
b5ec021
955bd0e
da454d2
6b23f9e
6c9bd4b
53fa088
fbe4764
98b9a30
993c9fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
require "jsontableschema/types/string" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it's something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's it, this is the main file that requires everything needed by the gem. There are other patterns (i.e. we could modularise, so you could only require |
||
require "jsontableschema/types/time" | ||
|
||
require "jsontableschema/field" | ||
require "jsontableschema/validate" | ||
require "jsontableschema/model" | ||
require "jsontableschema/data" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module JsonTableSchema | ||
class Field < Hash | ||
include JsonTableSchema::Helpers | ||
|
||
attr_reader :type_class | ||
|
||
def initialize(descriptor) | ||
self.merge! descriptor | ||
@type_class = get_type | ||
end | ||
|
||
def name | ||
self['name'] | ||
end | ||
|
||
def type | ||
self['type'] || 'string' | ||
end | ||
|
||
def format | ||
self['format'] || 'default' | ||
end | ||
|
||
def constraints | ||
self['constraints'] || {} | ||
end | ||
|
||
def cast_value(col) | ||
klass = get_class_for_type(type) | ||
converter = Kernel.const_get(klass).new(self) | ||
converter.cast(col) | ||
end | ||
|
||
private | ||
|
||
def get_type | ||
Object.const_get get_class_for_type(type) | ||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module JsonTableSchema | ||
VERSION = "0.1.0" | ||
VERSION = "0.2.0" | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my experience almost all interactions with schema related to casting go thru
schema.cast_row
- not sure this cast method needed at all. Also name could be confusing (schema.cast
- cast what? schema? data?). WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this was carried over from the old Python API - calling it
cast_rows
would probably make more sense