Module: FormObject::Integrations::Base::ClassMethods
- Included in:
- FormObject::Integrations::Base
- Defined in:
- lib/form_object/integrations/base.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
The default options to use for state machines using this integration.
Instance Method Summary collapse
-
#available? ⇒ Boolean
Whether this integration is available for the current library.
-
#extended(base) ⇒ Object
Extends the given object with any version overrides that are currently active.
-
#forms ⇒ Object
FormsCollection for this model.
-
#integration_name ⇒ Object
The name of the integration.
-
#locale_path ⇒ Object
The path to the locale file containing translations for this integration.
-
#matches?(klasses) ⇒ Boolean
Whether the integration should be used for the given class.
-
#matches_ancestors?(ancestors) ⇒ Boolean
Whether the integration should be used for the given list of ancestors.
-
#matching_ancestors ⇒ Object
The list of ancestor names that cause this integration to matched.
-
#version(name, &block) ⇒ Object
DSL for define version and version based methods.
-
#versions ⇒ Object
Defined versions.
Instance Attribute Details
#defaults ⇒ Object (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 |
#forms ⇒ Object
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_name ⇒ Object
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_path ⇒ Object
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_ancestors ⇒ Object
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 |
#versions ⇒ Object
Defined versions
81 82 83 |
# File 'lib/form_object/integrations/base.rb', line 81 def versions @versions ||= [] end |