Module: FormObject::Integrations::Base::ClassMethods

Included in:
FormObject::Integrations::Base
Defined in:
lib/form_object/integrations/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject (readonly)

The default options to use for state machines using this integration



6
7
8
# File 'lib/form_object/integrations/base.rb', line 6

def defaults
  @defaults
end

Instance Method Details

#available?Boolean

Whether this integration is available for the current library. This is only true if the ORM that the integration is for is currently defined.



44
45
46
# File 'lib/form_object/integrations/base.rb', line 44

def available?
  matching_ancestors.any? && Object.const_defined?(matching_ancestors[0].split('::')[0])
end

#extended(base) ⇒ Object

Extends the given object with any version overrides that are currently active



74
75
76
77
78
# File 'lib/form_object/integrations/base.rb', line 74

def extended(base)
  versions.each do |version|
     base.extend(version) if version.active?
  end
end

#formsObject

FormsCollection for this model

Example

class BaseForm < FormObject::Base
  model User
end

class TwitterForm < FormObject::Base
  model User, as: :twitter
end

@user = User.new
@base_form = @user.forms[:base]       # => instance of BaseForm
@twitter_form = @iser.forms[:twitter] # => instance of TwitterForm

Forms must have names. If the form name is not specified, it is taken from class name.



26
27
28
# File 'lib/form_object/integrations/base.rb', line 26

def forms
  nil
end

#integration_nameObject

The name of the integration



31
32
33
34
35
36
37
38
39
# File 'lib/form_object/integrations/base.rb', line 31

def integration_name
  @integration_name ||= begin
    name = self.name.split('::').last
    name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
    name.downcase!
    name.to_sym
  end
end

#locale_pathObject

The path to the locale file containing translations for this integration. This file will only exist for integrations that actually support i18n.



67
68
69
70
# File 'lib/form_object/integrations/base.rb', line 67

def locale_path
  path = "#{File.dirname(__FILE__)}/#{integration_name}/locale.rb"
  path if File.exists?(path)
end

#matches?(klasses) ⇒ Boolean

Whether the integration should be used for the given class.



54
55
56
57
# File 'lib/form_object/integrations/base.rb', line 54

def matches?(klasses)
  ancestor_names = klasses.map {|klass| klass.ancestors.map(&:name)}
  matches_ancestors?(ancestor_names)
end

#matches_ancestors?(ancestors) ⇒ Boolean

Whether the integration should be used for the given list of ancestors.



60
61
62
# File 'lib/form_object/integrations/base.rb', line 60

def matches_ancestors?(ancestors)
  (ancestors & matching_ancestors).any?
end

#matching_ancestorsObject

The list of ancestor names that cause this integration to matched.



49
50
51
# File 'lib/form_object/integrations/base.rb', line 49

def matching_ancestors
  []
end

#version(name, &block) ⇒ Object

DSL for define version and version based methods



87
88
89
90
91
# File 'lib/form_object/integrations/base.rb', line 87

def version( name, &block )
  mod = Module.new(&block)
  versions << mod
  mod
end

#versionsObject

Defined versions



81
82
83
# File 'lib/form_object/integrations/base.rb', line 81

def versions
  @versions ||= []
end