Skip to content
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

use activemodel’s API to setup attributes #15

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions easy_talk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Gem::Specification.new do |spec|

spec.require_paths = ['lib']

spec.add_dependency 'activemodel', '~> 7.0'
spec.add_dependency 'activesupport', '~> 7.0'
spec.add_dependency 'json-schema', '~> 4'
spec.add_dependency 'sorbet-runtime', '~> 0.5'
Expand Down
2 changes: 2 additions & 0 deletions lib/easy_talk/builders/object_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ def properties_from_schema_definition
end
end

# rubocop:disable Style/DoubleNegation
def add_required_property(property_name, options)
return if options.is_a?(Hash) && !!(options[:type].respond_to?(:nilable?) && options[:type].nilable?)

return if options.respond_to?(:optional?) && options.optional?

@required_properties << property_name
end
# rubocop:enable Style/DoubleNegation

def build_property(property_name, options)
if options.is_a?(EasyTalk::SchemaDefinition)
Expand Down
46 changes: 20 additions & 26 deletions lib/easy_talk/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'active_support/time'
require 'active_support/concern'
require 'active_support/json'
require 'active_model'
require 'json-schema'
require_relative 'builders/object_builder'
require_relative 'schema_definition'
Expand All @@ -20,44 +21,26 @@ module Model
#
# Example usage:
#
# class MyModel
# extend ClassMethods
# class Person
# include EasyTalk::Model
#
# define_schema do
# # schema definition goes here
# property :name, String, description: 'The person\'s name'
# property :age, Integer, description: 'The person\'s age'
# end
# end
#
# MyModel.json_schema #=> returns the JSON schema for MyModel
#
# MyModel.schema_definition #=> returns the unvalidated schema definition for MyModel
#
# MyModel.ref_template #=> returns the reference template for MyModel
#
# MyModel.inherits_schema? #=> returns false
#
# MyModel.schema #=> returns the validated schema for MyModel
#
# MyModel.schema_definition #=> returns the unvalidated schema definition for MyModel
#
# MyModel.json_schema #=> returns the JSON schema for MyModel
# Person.json_schema #=> returns the JSON schema for Person
# jim = Person.new(name: 'Jim', age: 30)
# jim.valid? #=> returns true
#
# @see SchemaDefinition
#
def self.included(base)
base.include ActiveModel::API # Include ActiveModel::API in the class including EasyTalk::Model
base.extend(ClassMethods)
end

# Initializes a new instance of the Model class.
#
# @param properties [Hash] The properties to set for the instance.
def initialize(properties = {})
properties.each do |key, value|
instance_variable_set("@#{key}", value)
self.class.class_eval { attr_reader key }
end
end

# Checks if the model is valid.
#
# This method calls the `validate_json` class method on the current class,
Expand Down Expand Up @@ -103,6 +86,14 @@ def function_name
name.humanize.titleize
end

def properties
@properties ||= begin
return unless schema[:properties].present?

schema[:properties].keys.map(&:to_sym)
end
end

# Validates the given JSON against the model's JSON schema.
#
# @param json [Hash] The JSON to validate.
Expand All @@ -127,6 +118,9 @@ def define_schema(&block)

@schema_definition = SchemaDefinition.new(name)
@schema_definition.instance_eval(&block)
attr_accessor(*properties)

@schema_defintion
end

# Returns the unvalidated schema definition for the model.
Expand Down
21 changes: 10 additions & 11 deletions spec/easy_talk/examples/user_routing_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.name
end

define_schema do
property 'user/:id', :object do
property 'user_id', :object do
description 'Get a user by id'
property :phrases, T::Array[String],
title: 'trigger phrase examples',
Expand All @@ -27,7 +27,7 @@ def self.name
end
property :path, String, description: 'The route path to get the user by id'
end
property 'user/:email', :object do
property 'user_email', :object do
description 'Get a user by email'
property :phrases, T::Array[String],
title: 'trigger phrase examples',
Expand All @@ -42,7 +42,7 @@ def self.name
end
property :path, String, const: 'user/:email', description: 'The route path to get the user by email'
end
property 'user/:id/authenticate', :object do
property 'user_id_authenticate', :object do
description 'Authenticate a user'
property :phrases, T::Array[String],
title: 'trigger phrase examples',
Expand All @@ -65,7 +65,7 @@ def self.name
{
"type": 'object',
"properties": {
"user/:id": {
"user_id": {
"type": 'object',
"description": 'Get a user by id',
"properties": {
Expand Down Expand Up @@ -105,7 +105,7 @@ def self.name
path
]
},
"user/:email": {
"user_email": {
"type": 'object',
"description": 'Get a user by email',
"properties": {
Expand Down Expand Up @@ -146,7 +146,7 @@ def self.name
path
]
},
"user/:id/authenticate": {
"user_id_authenticate": {
"type": 'object',
"description": 'Authenticate a user',
"properties": {
Expand Down Expand Up @@ -188,17 +188,16 @@ def self.name
]
}
},
"required": [
'user/:id',
'user/:email',
'user/:id/authenticate'
"required": %w[
user_id
user_email
user_id_authenticate
]
}
end

it 'returns a json schema for the book class' do
stub_const('UserRouting', user_routing)
puts UserRouting.json_schema.to_json
expect(UserRouting.json_schema).to include_json(expected_json_schema)
end
end
71 changes: 20 additions & 51 deletions spec/easy_talk/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,6 @@ def self.name
end

describe 'the schema' do
let(:expecnted_json_schema) do
{
type: 'object',
title: 'User',
properties: {
name: {
type: 'string'
},
age: {
type: 'integer'
}
}
}
end

it 'returns the validated internal representation of the schema' do
expect(user.schema).to be_a(Hash)
end
Expand Down Expand Up @@ -216,52 +201,36 @@ def self.name
}
end

let(:employee) { user.new(name: 'John', age: 21, email: { address: '[email protected]', verified: 'false' }) }

it 'returns the JSON schema' do
expect(user.json_schema).to include_json(expected_json_schema)
end
end
end

context 'with propert mapping' do
let(:user) do
Class.new do
include EasyTalk::Model

def self.name
'User'
end

define_schema do
title 'User'
property :name, String
property :age, Integer
property :email, String
end
it 'returns the model properties' do
expect(user.properties).to eq(%i[name age email])
end
end

let(:instance) do
user.new(name: 'John', age: 21, email: '[email protected]')
end

it 'maps name property to the instance of the class' do
expect(instance.name).to eq('John')
end
it "returns the model's name" do
expect(user.name).to eq('User')
end

it 'maps age property to the instance of the class' do
expect(instance.age).to eq(21)
end
# FIXME: This test is failing because the email property hash keys are strings.
pending "returns the model's properties' values" do
expect(employee.properties).to eq(name: 'John', age: 21, email: { address: '[email protected]', verified: 'false' })
end

it 'maps email property to the instance of the class' do
expect(instance.email).to eq('[email protected]')
end
it 'returns a property' do
expect(employee.name).to eq('John')
end

it 'checks if the instance is valid' do
expect(instance.valid?).to eq(true)
end
it 'returns a hash type property' do
expect(employee.email).to eq(address: '[email protected]', verified: 'false')
end

it 'maps all properties to the instance of the class' do
expect(instance.properties).to eq(name: 'John', age: 21, email: '[email protected]')
it 'is valid' do
expect(employee.valid?).to eq(true)
end
end
end
end
Loading