-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from brianstien/remove_active_attr_typecasting
Remove active attr typecasting
- Loading branch information
Showing
16 changed files
with
247 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require "active_remote/typecasting/big_decimal_typecaster" | ||
require "active_remote/typecasting/boolean" | ||
require "active_remote/typecasting/boolean_typecaster" | ||
require "active_remote/typecasting/date_time_typecaster" | ||
require "active_remote/typecasting/date_typecaster" | ||
require "active_remote/typecasting/float_typecaster" | ||
require "active_remote/typecasting/integer_typecaster" | ||
require "active_remote/typecasting/object_typecaster" | ||
require "active_remote/typecasting/string_typecaster" | ||
|
||
module ActiveRemote | ||
module Typecasting | ||
extend ActiveSupport::Concern | ||
|
||
TYPECASTER_MAP = { | ||
BigDecimal => ::ActiveRemote::Typecasting::BigDecimalTypecaster, | ||
Boolean => ::ActiveRemote::Typecasting::BooleanTypecaster, | ||
Date => ::ActiveRemote::Typecasting::DateTypecaster, | ||
DateTime => ::ActiveRemote::Typecasting::DateTimeTypecaster, | ||
Float => ::ActiveRemote::Typecasting::FloatTypecaster, | ||
Integer => ::ActiveRemote::Typecasting::IntegerTypecaster, | ||
Object => ::ActiveRemote::Typecasting::ObjectTypecaster, | ||
String => ::ActiveRemote::Typecasting::StringTypecaster | ||
}.freeze | ||
|
||
private | ||
|
||
def attribute=(name, value) | ||
return super if value.nil? | ||
|
||
typecaster = _attribute_typecaster(name) | ||
return super unless typecaster | ||
|
||
super(name, typecaster.call(value)) | ||
end | ||
|
||
def _attribute_typecaster(attribute_name) | ||
self.class.attributes[attribute_name][:typecaster] || _typecaster_for(attribute_name) | ||
end | ||
|
||
def _typecaster_for(attribute_name) | ||
type = self.class.attributes[attribute_name][:type] | ||
return nil unless type | ||
|
||
TYPECASTER_MAP[type] | ||
end | ||
|
||
module ClassMethods | ||
def inspect | ||
inspected_attributes = attribute_names.sort.map { |name| "#{name}: #{_attribute_type(name)}" } | ||
attributes_list = "(#{inspected_attributes.join(", ")})" unless inspected_attributes.empty? | ||
"#{name}#{attributes_list}" | ||
end | ||
|
||
def _attribute_type(attribute_name) | ||
attributes[attribute_name][:type] || Object | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require "bigdecimal" | ||
require "bigdecimal/util" | ||
require "active_support/core_ext/big_decimal/conversions" | ||
|
||
module ActiveRemote | ||
module Typecasting | ||
class BigDecimalTypecaster | ||
def self.call(value) | ||
if value.is_a?(BigDecimal) | ||
value | ||
elsif value.is_a?(Rational) | ||
value.to_f.to_d | ||
elsif value.respond_to?(:to_d) | ||
value.to_d | ||
else | ||
BigDecimal.new(value.to_s) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module ActiveRemote | ||
module Typecasting | ||
# For use in :type dsl option | ||
class Boolean | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module ActiveRemote | ||
module Typecasting | ||
class BooleanTypecaster | ||
FALSE_VALUES = ["n", "N", "no", "No", "NO", "false", "False", "FALSE", "off", "Off", "OFF", "f", "F"] | ||
|
||
def self.call(value) | ||
case value | ||
when *FALSE_VALUES then false | ||
when Numeric, /^\-?[0-9]/ then !value.to_f.zero? | ||
else value.present? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ActiveRemote | ||
module Typecasting | ||
class DateTimeTypecaster | ||
def self.call(value) | ||
value.to_datetime if value.respond_to?(:to_datetime) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module ActiveRemote | ||
module Typecasting | ||
class DateTypecaster | ||
def self.call(value) | ||
value.to_date if value.respond_to?(:to_date) | ||
rescue NoMethodError, ArgumentError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ActiveRemote | ||
module Typecasting | ||
class FloatTypecaster | ||
def self.call(value) | ||
value.to_f if value.respond_to?(:to_f) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module ActiveRemote | ||
module Typecasting | ||
class IntegerTypecaster | ||
def self.call(value) | ||
value.to_i if value.respond_to?(:to_i) | ||
rescue FloatDomainError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ActiveRemote | ||
module Typecasting | ||
class ObjectTypecaster | ||
def self.call(value) | ||
value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ActiveRemote | ||
module Typecasting | ||
class StringTypecaster | ||
def self.call(value) | ||
value.to_s if value.respond_to?(:to_s) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'spec_helper' | ||
describe ::ActiveRemote::Typecasting do | ||
let(:test_class) { ::TypecastedAuthor } | ||
|
||
describe "boolean" do | ||
it "casts to boolean" do | ||
record = test_class.new(:writes_fiction => "no") | ||
expect(record.writes_fiction).to eq(false) | ||
end | ||
end | ||
|
||
describe "datetime" do | ||
it "casts to datetime" do | ||
record = test_class.new(:birthday => "2016-01-01") | ||
expect(record.birthday).to eq(DateTime.parse("2016-01-01")) | ||
end | ||
end | ||
|
||
describe "float" do | ||
it "casts to float" do | ||
record = test_class.new(:net_sales => "2000.20") | ||
expect(record.net_sales).to eq(2000.2) | ||
end | ||
end | ||
|
||
describe "integer" do | ||
it "casts to integer" do | ||
record = test_class.new(:age => "40") | ||
expect(record.age).to eq(40) | ||
end | ||
end | ||
|
||
describe "string" do | ||
it "casts to string" do | ||
record = test_class.new(:guid => 1000) | ||
expect(record.guid).to eq("1000") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class TypecastedAuthor < ::ActiveRemote::Base | ||
attribute :guid, :type => String | ||
attribute :name, :typecaster => StringTypecaster | ||
attribute :age, :type => Integer | ||
attribute :birthday, :type => DateTime | ||
attribute :writes_fiction, :type => Boolean | ||
attribute :net_sales, :type => Float | ||
end |