Class: Dry::Resource
- Inherits:
-
Object
- Object
- Dry::Resource
- 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
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #attrs_options ⇒ Object
- #attrs_read ⇒ Object
- #attrs_write ⇒ Object
- #destroy? ⇒ Boolean
- #each_form_fields ⇒ Object
- #edit? ⇒ Boolean
-
#initialize(model, options = {}) ⇒ Resource
constructor
A new instance of Resource.
- #new? ⇒ Boolean
- #plural_name ⇒ Object
- #relations ⇒ Object
- #relations? ⇒ Boolean
- #show? ⇒ Boolean
- #singular_name ⇒ Object
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, = {} @model = model @options = DEFAULT_OPTIONS.merge end |
Instance Attribute Details
#model ⇒ Object (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_options ⇒ Object
71 72 73 |
# File 'lib/dry/resource.rb', line 71 def @options[:attrs_options] end |
#attrs_read ⇒ Object
63 64 65 |
# File 'lib/dry/resource.rb', line 63 def attrs_read @options[:attrs_read] end |
#attrs_write ⇒ Object
67 68 69 |
# File 'lib/dry/resource.rb', line 67 def attrs_write @options[:attrs_write] end |
#destroy? ⇒ Boolean
59 60 61 |
# File 'lib/dry/resource.rb', line 59 def destroy? has_route_for? :destroy end |
#each_form_fields ⇒ Object
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
55 56 57 |
# File 'lib/dry/resource.rb', line 55 def edit? has_route_for? :edit end |
#new? ⇒ Boolean
51 52 53 |
# File 'lib/dry/resource.rb', line 51 def new? has_route_for? :new end |
#plural_name ⇒ Object
35 36 37 |
# File 'lib/dry/resource.rb', line 35 def plural_name model_name.plural end |
#relations ⇒ Object
43 44 45 |
# File 'lib/dry/resource.rb', line 43 def relations @options[:relations] end |
#relations? ⇒ Boolean
39 40 41 |
# File 'lib/dry/resource.rb', line 39 def relations? @options[:relations].any? end |
#show? ⇒ Boolean
47 48 49 |
# File 'lib/dry/resource.rb', line 47 def show? has_route_for? :show end |
#singular_name ⇒ Object
31 32 33 |
# File 'lib/dry/resource.rb', line 31 def singular_name model_name.singular end |