Class: Dry::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dry/resource.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  attrs_read:     [],
  attrs_write:    [],
  attrs_options:  {},
  relations:      [],
  routes:         []
}
ATTR_TYPE_TO_FORM_FIELD =
{
  boolean:  :check_box,
  decimal:  :number_field,
  integer:  :number_field,
  string:   :text_field,
  text:     :text_area
}
ATTR_RELATION_PATTERN =
/_id\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options = {}) ⇒ Resource

Returns a new instance of Resource.



26
27
28
29
# File 'lib/dry/resource.rb', line 26

def initialize model, options = {}
  @model    = model
  @options  = DEFAULT_OPTIONS.merge options
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



24
25
26
# File 'lib/dry/resource.rb', line 24

def model
  @model
end

Instance Method Details

#attrs_optionsObject



71
72
73
# File 'lib/dry/resource.rb', line 71

def attrs_options
  @options[:attrs_options]
end

#attrs_readObject



63
64
65
# File 'lib/dry/resource.rb', line 63

def attrs_read
  @options[:attrs_read]
end

#attrs_writeObject



67
68
69
# File 'lib/dry/resource.rb', line 67

def attrs_write
  @options[:attrs_write]
end

#destroy?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/dry/resource.rb', line 59

def destroy?
  has_route_for? :destroy
end

#each_form_fieldsObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/dry/resource.rb', line 75

def each_form_fields
  attrs_write.each do |e|
    if e =~ ATTR_RELATION_PATTERN
      yield e,
        @model.reflections[e.to_s.sub ATTR_RELATION_PATTERN, ''].klass
    else
      yield e, form_field_for(e)
    end
  end
end

#edit?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/dry/resource.rb', line 55

def edit?
  has_route_for? :edit
end

#new?Boolean

Returns:

  • (Boolean)


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

def new?
  has_route_for? :new
end

#plural_nameObject



35
36
37
# File 'lib/dry/resource.rb', line 35

def plural_name
  model_name.plural
end

#relationsObject



43
44
45
# File 'lib/dry/resource.rb', line 43

def relations
  @options[:relations]
end

#relations?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dry/resource.rb', line 39

def relations?
  @options[:relations].any?
end

#show?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/dry/resource.rb', line 47

def show?
  has_route_for? :show
end

#singular_nameObject



31
32
33
# File 'lib/dry/resource.rb', line 31

def singular_name
  model_name.singular
end