pure_form 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7025ebff1558b57eb3dc586e5f6fcbb4af7c6680
4
- data.tar.gz: 9a923ef9e28728fd0ce013a7c2d7f7aba6466cec
3
+ metadata.gz: 90f8472fd99ffd4d20924e0c56ebeb8cbb85243e
4
+ data.tar.gz: 518154b27102d26a3d7e4589a58b853a355934c2
5
5
  SHA512:
6
- metadata.gz: 9094f32da6ee09629c3da92d650ba85351b65f8ad949993d41aeeb1d4a9fc68b8c0da8b8efd6a97c63b6dc86c6f3044f3877b31af94bb0c12462982dda8f970e
7
- data.tar.gz: c7800172e42789f6d37a445a58c9df83acadaf8f0dd05ab2353e9d01062217cf37d13970f8d47b1b93803b484362976a0ccd8b59c78d1b0a00533ca4e169dac4
6
+ metadata.gz: d0414269400b2bd657fea2bf6e44a0ab844fd1b9201f494de5f78f8681c84d65751287755ae088cc3255b21a6f4d3263ba8af0e2d7eb0637ef861fb4c9377b5d
7
+ data.tar.gz: d718fb38262b2484126a27e8578315cfae794280a2c321a80348177a30b68347a799e99f3ed7d3290c3a82c199e7b6ce6fe623d57be0d29fe988d2c4212f6e43
@@ -1,9 +1,9 @@
1
1
  module PureForm
2
2
  class Assignment
3
- attr_reader :form, :attributes
3
+ attr_reader :form, :attributes, :options
4
4
 
5
- def initialize(form, attributes)
6
- @form, @attributes = form, attributes
5
+ def initialize(form, attributes, **options)
6
+ @form, @attributes, @options = form, attributes, options
7
7
  end
8
8
 
9
9
  def perform
@@ -16,14 +16,17 @@ module PureForm
16
16
  end
17
17
 
18
18
  flush_complex_attributes
19
+
20
+ form.attributes
19
21
  end
20
22
 
21
23
  private
22
24
 
23
25
  def assign_attribute(attribute_name, value)
24
- setter = "#{attribute_name}="
25
- fail Errors::UnknownAttributeError.build(attribute_name) unless form.respond_to?(setter)
26
- form.public_send setter, value
26
+ method_name = "#{attribute_name}="
27
+ call_form_method_or_fail method_name, value do
28
+ fail Errors::UnknownAttributeError.build(attribute_name)
29
+ end
27
30
  end
28
31
 
29
32
  def assign_complex_attribute(complex_attribute_name, value)
@@ -41,8 +44,17 @@ module PureForm
41
44
  def flush_complex_attributes
42
45
  complex_attributes.each do |key, values|
43
46
  method_name = "set_complex_#{key}_value"
44
- raise Errors::MissingComplexAttributeError.build(key) unless form.respond_to?(method_name)
45
- form.public_send method_name, *values
47
+ call_form_method_or_fail method_name, *values do
48
+ raise Errors::MissingComplexAttributeError.build(key)
49
+ end
50
+ end
51
+ end
52
+
53
+ def call_form_method_or_fail(method_name, *args)
54
+ if form.respond_to?(method_name)
55
+ form.public_send(method_name, *args)
56
+ else
57
+ yield unless options[:ignore_undefined]
46
58
  end
47
59
  end
48
60
  end
@@ -38,10 +38,12 @@ module PureForm
38
38
  context.instance_eval do
39
39
  define_method name, &block
40
40
  end
41
+
42
+ nil
41
43
  end
42
44
 
43
45
  def setter_proc
44
- attribute_name = self.name
46
+ attribute_name = name
45
47
  type = value_type
46
48
  ->(value){ store_attribute(attribute_name, type.typecast(value)) }
47
49
  end
@@ -53,19 +55,20 @@ module PureForm
53
55
  end
54
56
 
55
57
  def getter_proc
56
- attribute_name = self.name
58
+ attribute_name = name
57
59
  ->{ read_attribute(attribute_name) }
58
60
  end
59
61
 
60
62
  def predicate_proc
61
- attribute_name = self.name
63
+ attribute_name = name
62
64
  ->{ public_send(attribute_name).present? }
63
65
  end
64
66
 
65
67
  def build_value_type
66
68
  return Types::BaseType.new(options) unless options.key?(:type)
67
- type = options.delete(:type)
68
- "PureForm::Types::#{type.to_s.classify}Type".constantize.new(options)
69
+ type = options.delete(:type).to_s
70
+ type = "date_time" if type == "datetime"
71
+ "PureForm::Types::#{type.camelize}Type".constantize.new(options)
69
72
  end
70
73
  end
71
74
  end
@@ -4,13 +4,19 @@ module PureForm
4
4
  class Base
5
5
  include ActiveModel::Model
6
6
 
7
- class_attribute :attributes, instance_accessor: false, instance_predicate: false
7
+ class Boolean
8
+ def self.to_s
9
+ "Boolean"
10
+ end
11
+ end
12
+
13
+ class_attribute :defined_attributes, instance_accessor: false, instance_predicate: false
8
14
 
9
15
  class << self
10
16
  def attribute(name, **options)
11
17
  attribute = Attribute.new(self, name, options)
12
- self.attributes ||= HashWithIndifferentAccess.new
13
- self.attributes = attributes.merge(name => attribute)
18
+ self.defined_attributes ||= HashWithIndifferentAccess.new
19
+ self.defined_attributes = defined_attributes.merge(attribute.name => attribute)
14
20
  attribute.define
15
21
  end
16
22
 
@@ -25,9 +31,21 @@ module PureForm
25
31
  end
26
32
  end
27
33
 
34
+ def copy_attributes_from(model, **options)
35
+ raise ArgumentError unless model < ActiveRecord::Base
36
+
37
+ included = Array.wrap(options.fetch(:only){ model.column_names }).map(&:to_s)
38
+ excluded = Array.wrap(options[:except]).map(&:to_s)
39
+
40
+ model.columns.each do |column|
41
+ next if !column.name.in?(included) || column.name.in?(excluded)
42
+ attribute column.name, type: column.type
43
+ end
44
+ end
45
+
28
46
  def default_values
29
- return {} unless attributes
30
- @default_values ||= attributes.each_with_object({}) do |(name, attribute), defaults|
47
+ return {} unless defined_attributes
48
+ @default_values ||= defined_attributes.each_with_object({}) do |(name, attribute), defaults|
31
49
  if attribute.options.key?(:default)
32
50
  defaults[name] = attribute.options.fetch(:default)
33
51
  end
@@ -50,10 +68,14 @@ module PureForm
50
68
  Assignment.new(self, attributes).perform
51
69
  end
52
70
 
71
+ def assign_defined_attributes(attributes)
72
+ Assignment.new(self, attributes, ignore_undefined: true).perform
73
+ end
74
+
53
75
  alias_method :attributes=, :assign_attributes
54
76
 
55
77
  def attributes
56
- self.class.attributes.each_with_object({}) do |(name, _), attributes|
78
+ self.class.defined_attributes.each_with_object({}) do |(name, _), attributes|
57
79
  attributes[name] = public_send(name)
58
80
  end.with_indifferent_access
59
81
  end
@@ -75,11 +97,11 @@ module PureForm
75
97
  end
76
98
 
77
99
  def store_attribute(name, value)
78
- attributes_store.store name, value
100
+ attributes_store.store name.to_s, value
79
101
  end
80
102
 
81
103
  def read_attribute(name)
82
- attributes_store[name]
104
+ attributes_store[name.to_s]
83
105
  end
84
106
  end
85
107
  end
@@ -1,10 +1,11 @@
1
1
  module PureForm
2
2
  module Types
3
- autoload :BaseType, "pure_form/types/base"
4
- autoload :StringType, "pure_form/types/string"
5
- autoload :IntegerType, "pure_form/types/integer"
6
- autoload :FloatType, "pure_form/types/float"
7
- autoload :BooleanType, "pure_form/types/boolean"
8
- autoload :DateType, "pure_form/types/date"
3
+ autoload :BaseType, "pure_form/types/base"
4
+ autoload :StringType, "pure_form/types/string"
5
+ autoload :IntegerType, "pure_form/types/integer"
6
+ autoload :FloatType, "pure_form/types/float"
7
+ autoload :BooleanType, "pure_form/types/boolean"
8
+ autoload :DateType, "pure_form/types/date"
9
+ autoload :DateTimeType, "pure_form/types/date_time"
9
10
  end
10
11
  end
@@ -0,0 +1,17 @@
1
+ module PureForm
2
+ module Types
3
+ class DateTimeType < BaseType
4
+ def typecast(value)
5
+ value.to_datetime
6
+ rescue TypeError, ArgumentError, NoMethodError
7
+ nil
8
+ end
9
+
10
+ def complex_typecast(*args)
11
+ DateTime.new(*args)
12
+ rescue TypeError, ArgumentError, NoMethodError
13
+ nil
14
+ end
15
+ end
16
+ end
17
+ end
@@ -2,7 +2,7 @@ module PureForm
2
2
  module Types
3
3
  class FloatType < BaseType
4
4
  def typecast(value)
5
- value.to_f
5
+ value.blank?? nil : value.to_f
6
6
  rescue NoMethodError
7
7
  nil
8
8
  end
@@ -2,7 +2,7 @@ module PureForm
2
2
  module Types
3
3
  class IntegerType < BaseType
4
4
  def typecast(value)
5
- value.to_i
5
+ value.blank?? nil : value.to_i
6
6
  rescue NoMethodError
7
7
  nil
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module PureForm
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pure_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pravosud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-27 00:00:00.000000000 Z
11
+ date: 2014-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -44,6 +44,7 @@ files:
44
44
  - lib/pure_form/types/base.rb
45
45
  - lib/pure_form/types/boolean.rb
46
46
  - lib/pure_form/types/date.rb
47
+ - lib/pure_form/types/date_time.rb
47
48
  - lib/pure_form/types/float.rb
48
49
  - lib/pure_form/types/integer.rb
49
50
  - lib/pure_form/types/string.rb
@@ -68,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
69
  version: '0'
69
70
  requirements: []
70
71
  rubyforge_project:
71
- rubygems_version: 2.2.2
72
+ rubygems_version: 2.4.3
72
73
  signing_key:
73
74
  specification_version: 4
74
75
  summary: ''