Skip to content

split Attribute class to separate file #6

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 2 additions & 32 deletions lib/simple_form_object.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "simple_form_object/version"
require_relative "simple_form_object/attribute"
require_relative "simple_form_object/version"
require "active_model"
require "active_support"

Expand Down Expand Up @@ -45,35 +46,4 @@ def attributes
end
attribs
end

class Attribute
def initialize(name, type = nil, options)
@name = name
@type = type || :string
@options = options

extract_options
end

attr_accessor :name, :type, :options

def fake_column
self
end

def apply_default_to(form)
if form.send(@name).nil?
form.send("#{@name}=", @default) if @apply_default
end
end

private

def extract_options
@apply_default = true
@default = options.fetch(:default) { @apply_default = false; nil }
@skip_validations = options.fetch(:skip_validations, false)
end
end

end
31 changes: 31 additions & 0 deletions lib/simple_form_object/attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module SimpleFormObject
class Attribute
def initialize(name, type = nil, options)
@name = name
@type = type || :string
@options = options

extract_options
end

attr_accessor :name, :type, :options

def fake_column
self
end

def apply_default_to(form)
if form.send(@name).nil?
form.send("#{@name}=", @default) if @apply_default
end
end

private

def extract_options
@apply_default = true
@default = options.fetch(:default) { @apply_default = false; nil }
@skip_validations = options.fetch(:skip_validations, false)
end
end
end