Class: Supplejack::StoryItemRelation

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/supplejack/story_item_relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Request

#delete, #get, #patch, #post, #put

Constructor Details

#initialize(story) ⇒ StoryItemRelation

Returns a new instance of StoryItemRelation.



16
17
18
19
20
# File 'lib/supplejack/story_item_relation.rb', line 16

def initialize(story)
  @story = story
  items = story.attributes.try(:fetch, :contents, nil) || []
  build_items(items)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Any method missing on this class is delegated to the StoryItems objects array so that the developer can easily execute any Array method on the StoryItemRelation

Examples:

story.each ....     => Iterate through the StoryItem objects array
story.size          => Get the size of the StoryItem objects array


69
70
71
# File 'lib/supplejack/story_item_relation.rb', line 69

def method_missing(method, *args, &block)
  all.send(method, *args, &block)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



14
15
16
# File 'lib/supplejack/story_item_relation.rb', line 14

def errors
  @errors
end

#storyObject (readonly)

Returns the value of attribute story.



14
15
16
# File 'lib/supplejack/story_item_relation.rb', line 14

def story
  @story
end

Instance Method Details

#allObject



22
23
24
# File 'lib/supplejack/story_item_relation.rb', line 22

def all
  @items
end

#build(attributes = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/supplejack/story_item_relation.rb', line 26

def build(attributes = {})
  story_item = Supplejack::StoryItem.new(attributes.merge(story_id: story.id, api_key: story.api_key))

  @items << story_item

  story_item
end

#create(attributes = {}) ⇒ Object



34
35
36
37
38
# File 'lib/supplejack/story_item_relation.rb', line 34

def create(attributes = {})
  story_item = build(attributes)

  story_item.save
end

#find(id) ⇒ Object



54
55
56
# File 'lib/supplejack/story_item_relation.rb', line 54

def find(id)
  @items.detect{|i| i.id == id.to_i}
end

#move_item(item_id, position) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/supplejack/story_item_relation.rb', line 40

def move_item(item_id, position)
  begin
    response = post("/stories/#{story.id}/items/#{item_id}/moves", {api_key: story.api_key}, {item_id: item_id, position: position})

    build_items(response)

    true
  rescue StandardError => e
    @errors = e.inspect

    false
  end
end

#to_jsonObject



58
59
60
# File 'lib/supplejack/story_item_relation.rb', line 58

def to_json
  all.to_json
end