Module: HoboFields

Extended by:
HoboFields
Included in:
HoboFields
Defined in:
lib/hobo_fields.rb,
lib/hobo_fields/model.rb,
lib/hobo_fields/railtie.rb,
lib/hobo_fields/types/text.rb,
lib/hobo_fields/sanitize_html.rb,
lib/hobo_fields/model/field_spec.rb,
lib/hobo_fields/model/index_spec.rb,
lib/hobo_fields/types/enum_string.rb,
lib/hobo_fields/types/html_string.rb,
lib/hobo_fields/types/email_address.rb,
lib/hobo_fields/types/textile_string.rb,
lib/hobo_fields/field_declaration_dsl.rb,
lib/hobo_fields/types/lifecycle_state.rb,
lib/hobo_fields/types/markdown_string.rb,
lib/hobo_fields/types/password_string.rb,
lib/hobo_fields/types/raw_html_string.rb,
lib/hobo_fields/types/serialized_object.rb,
lib/hobo_fields/types/raw_markdown_string.rb

Defined Under Namespace

Modules: Model, SanitizeHtml, Types Classes: FieldDeclarationDsl, Railtie

Constant Summary collapse

VERSION =
File.read(File.expand_path('../../VERSION', __FILE__)).strip
PLAIN_TYPES =
{
  :boolean       => Hobo::Boolean,
  :date          => Date,
  :datetime      => ActiveSupport::TimeWithZone,
  :time          => Time,
  :integer       => Integer,
  :decimal       => BigDecimal,
  :float         => Float,
  :string        => String
}
ALIAS_TYPES =
{
  Fixnum => "integer",
  Bignum => "integer"
}
STANDARD_TYPES =

Provide a lookup for these rather than loading them all preemptively

{
  :raw_html      => "RawHtmlString",
  :html          => "HtmlString",
  :raw_markdown  => "RawMarkdownString",
  :markdown      => "MarkdownString",
  :textile       => "TextileString",
  :password      => "PasswordString",
  :text          => "Text",
  :email_address => "EmailAddress",
  :serialized    => "SerializedObject"
}
@@root =
Pathname.new File.expand_path('../..', __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#field_typesObject (readonly)

Returns the value of attribute field_types.



51
52
53
# File 'lib/hobo_fields.rb', line 51

def field_types
  @field_types
end

Class Method Details

.rootObject



15
# File 'lib/hobo_fields.rb', line 15

def self.root; @@root; end

Instance Method Details

#can_wrap?(type, val) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
# File 'lib/hobo_fields.rb', line 66

def can_wrap?(type, val)
  col_type = type::COLUMN_TYPE
  return false if val.blank? && (col_type == :integer || col_type == :float || col_type == :decimal)
  klass = Object.instance_method(:class).bind(val).call # Make sure we get the *real* class
  init_method = type.instance_method(:initialize)
  [-1,1].include?(init_method.arity) &&
    init_method.owner != Object.instance_method(:initialize).owner &&
    !@never_wrap_types.any? { |c| klass <= c }
end

#never_wrap(type) ⇒ Object



76
77
78
# File 'lib/hobo_fields.rb', line 76

def never_wrap(type)
  @never_wrap_types << type
end

#plain_type?(type_name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/hobo_fields.rb', line 84

def plain_type?(type_name)
  type_name.in?(PLAIN_TYPES)
end

#register_type(name, klass) ⇒ Object



80
81
82
# File 'lib/hobo_fields.rb', line 80

def register_type(name, klass)
  field_types[name] = klass
end

#standard_class(name) ⇒ Object



88
89
90
91
# File 'lib/hobo_fields.rb', line 88

def standard_class(name)
  class_name = STANDARD_TYPES[name]
  "HoboFields::Types::#{class_name}".constantize if class_name
end

#to_class(type) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/hobo_fields.rb', line 53

def to_class(type)
  if type.is_one_of?(Symbol, String)
    type = type.to_sym
    field_types[type] || standard_class(type)
  else
    type # assume it's already a class
  end
end

#to_name(type) ⇒ Object



62
63
64
# File 'lib/hobo_fields.rb', line 62

def to_name(type)
  field_types.key(type) || ALIAS_TYPES[type]
end